/demos/presence/html/js/stomp-0.1.0.js

https://github.com/progrium/nullmq · JavaScript · 193 lines · 193 code · 0 blank · 0 comment · 38 complexity · 3ecf13017c847dca3d496e3a6cca0281 MD5 · raw file

  1. (function() {
  2. var Client, Stomp;
  3. var __hasProp = Object.prototype.hasOwnProperty, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  4. Stomp = {
  5. WebSocket: typeof WebSocket !== "undefined" && WebSocket !== null ? WebSocket : null,
  6. frame: function(command, headers, body) {
  7. if (headers == null) {
  8. headers = [];
  9. }
  10. if (body == null) {
  11. body = '';
  12. }
  13. return {
  14. command: command,
  15. headers: headers,
  16. body: body,
  17. id: headers.id,
  18. receipt: headers.receipt,
  19. transaction: headers.transaction,
  20. destination: headers.destination,
  21. subscription: headers.subscription,
  22. error: null,
  23. toString: function() {
  24. var lines, name, value;
  25. lines = [command];
  26. for (name in headers) {
  27. if (!__hasProp.call(headers, name)) continue;
  28. value = headers[name];
  29. lines.push("" + name + ": " + value);
  30. }
  31. lines.push('\n' + body);
  32. return lines.join('\n');
  33. }
  34. };
  35. },
  36. unmarshal: function(data) {
  37. var body, chr, command, divider, headerLines, headers, i, idx, line, trim, _ref, _ref2, _ref3;
  38. divider = data.search(/\n\n/);
  39. headerLines = data.substring(0, divider).split('\n');
  40. command = headerLines.shift();
  41. headers = {};
  42. body = '';
  43. trim = function(str) {
  44. return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
  45. };
  46. line = idx = null;
  47. for (i = 0, _ref = headerLines.length; 0 <= _ref ? i < _ref : i > _ref; 0 <= _ref ? i++ : i--) {
  48. line = headerLines[i];
  49. idx = line.indexOf(':');
  50. headers[trim(line.substring(0, idx))] = trim(line.substring(idx + 1));
  51. }
  52. chr = null;
  53. for (i = _ref2 = divider + 2, _ref3 = data.length; _ref2 <= _ref3 ? i < _ref3 : i > _ref3; _ref2 <= _ref3 ? i++ : i--) {
  54. chr = data.charAt(i);
  55. if (chr === '\0') {
  56. break;
  57. }
  58. body += chr;
  59. }
  60. return Stomp.frame(command, headers, body);
  61. },
  62. marshal: function(command, headers, body) {
  63. return Stomp.frame(command, headers, body).toString() + '\0';
  64. },
  65. client: function(url) {
  66. return new Client(url);
  67. }
  68. };
  69. Client = (function() {
  70. function Client(url) {
  71. this.url = url;
  72. this.counter = 0;
  73. this.connected = false;
  74. this.subscriptions = {};
  75. }
  76. Client.prototype._transmit = function(command, headers, body) {
  77. var out;
  78. out = Stomp.marshal(command, headers, body);
  79. if (typeof this.debug === "function") {
  80. this.debug(">>> " + out);
  81. }
  82. return this.ws.send(out);
  83. };
  84. Client.prototype.connect = function(login_, passcode_, connectCallback, errorCallback) {
  85. if (typeof this.debug === "function") {
  86. this.debug("Opening Web Socket...");
  87. }
  88. this.ws = new Stomp.WebSocket(this.url);
  89. this.ws.onmessage = __bind(function(evt) {
  90. var frame, onreceive;
  91. if (typeof this.debug === "function") {
  92. this.debug('<<< ' + evt.data);
  93. }
  94. frame = Stomp.unmarshal(evt.data);
  95. if (frame.command === "CONNECTED" && connectCallback) {
  96. this.connected = true;
  97. return connectCallback(frame);
  98. } else if (frame.command === "MESSAGE") {
  99. onreceive = this.subscriptions[frame.headers.subscription];
  100. return typeof onreceive === "function" ? onreceive(frame) : void 0;
  101. }
  102. }, this);
  103. this.ws.onclose = __bind(function() {
  104. var msg;
  105. msg = "Whoops! Lost connection to " + this.url;
  106. if (typeof this.debug === "function") {
  107. this.debug(msg);
  108. }
  109. return typeof errorCallback === "function" ? errorCallback(msg) : void 0;
  110. }, this);
  111. this.ws.onopen = __bind(function() {
  112. if (typeof this.debug === "function") {
  113. this.debug('Web Socket Opened...');
  114. }
  115. return this._transmit("CONNECT", {
  116. login: login_,
  117. passcode: passcode_
  118. });
  119. }, this);
  120. return this.connectCallback = connectCallback;
  121. };
  122. Client.prototype.disconnect = function(disconnectCallback) {
  123. this._transmit("DISCONNECT");
  124. this.ws.close();
  125. this.connected = false;
  126. return typeof disconnectCallback === "function" ? disconnectCallback() : void 0;
  127. };
  128. Client.prototype.send = function(destination, headers, body) {
  129. if (headers == null) {
  130. headers = {};
  131. }
  132. if (body == null) {
  133. body = '';
  134. }
  135. headers.destination = destination;
  136. return this._transmit("SEND", headers, body);
  137. };
  138. Client.prototype.subscribe = function(destination, callback, headers) {
  139. var id;
  140. if (headers == null) {
  141. headers = {};
  142. }
  143. id = "sub-" + this.counter++;
  144. headers.destination = destination;
  145. headers.id = id;
  146. this.subscriptions[id] = callback;
  147. this._transmit("SUBSCRIBE", headers);
  148. return id;
  149. };
  150. Client.prototype.unsubscribe = function(id, headers) {
  151. if (headers == null) {
  152. headers = {};
  153. }
  154. headers.id = id;
  155. delete this.subscriptions[id];
  156. return this._transmit("UNSUBSCRIBE", headers);
  157. };
  158. Client.prototype.begin = function(transaction, headers) {
  159. if (headers == null) {
  160. headers = {};
  161. }
  162. headers.transaction = transaction;
  163. return this._transmit("BEGIN", headers);
  164. };
  165. Client.prototype.commit = function(transaction, headers) {
  166. if (headers == null) {
  167. headers = {};
  168. }
  169. headers.transaction = transaction;
  170. return this._transmit("COMMIT", headers);
  171. };
  172. Client.prototype.abort = function(transaction, headers) {
  173. if (headers == null) {
  174. headers = {};
  175. }
  176. headers.transaction = transaction;
  177. return this._transmit("ABORT", headers);
  178. };
  179. Client.prototype.ack = function(message_id, headers) {
  180. if (headers == null) {
  181. headers = {};
  182. }
  183. headers["message-id"] = message_id;
  184. return this._transmit("ACK", headers);
  185. };
  186. return Client;
  187. })();
  188. if (typeof window !== "undefined" && window !== null) {
  189. window.Stomp = Stomp;
  190. } else {
  191. exports.Stomp = Stomp;
  192. }
  193. }).call(this);