Guest User

Untitled

a guest
Mar 21st, 2014
161
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Generated by CoffeeScript 1.6.3
  2. /*
  3. Stomp Over WebSocket http://www.jmesnil.net/stomp-websocket/doc/ | Apache License V2.0
  4.  
  5. Copyright (C) 2010-2013 [Jeff Mesnil](http://jmesnil.net/)
  6. Copyright (C) 2012 [FuseSource, Inc.](http://fusesource.com)
  7. */
  8.  
  9.  
  10. (function() {
  11. var Byte, Client, Frame, Stomp,
  12. __hasProp = {}.hasOwnProperty,
  13. __slice = [].slice;
  14.  
  15. Byte = {
  16. LF: '\x0A',
  17. NULL: '\x00'
  18. };
  19.  
  20. Frame = (function() {
  21. var unmarshallSingle;
  22.  
  23. function Frame(command, headers, body) {
  24. this.command = command;
  25. this.headers = headers != null ? headers : {};
  26. this.body = body != null ? body : '';
  27. }
  28.  
  29. Frame.prototype.toString = function() {
  30. var lines, name, value, _ref;
  31. lines = [this.command];
  32. _ref = this.headers;
  33. for (name in _ref) {
  34. if (!__hasProp.call(_ref, name)) continue;
  35. value = _ref[name];
  36. lines.push("" + name + ":" + value);
  37. }
  38. if (this.body) {
  39. lines.push("content-length:" + (Frame.sizeOfUTF8(this.body)));
  40. }
  41. lines.push(Byte.LF + this.body);
  42. return lines.join(Byte.LF);
  43. };
  44.  
  45. Frame.sizeOfUTF8 = function(s) {
  46. if (s) {
  47. return encodeURI(s).split(/%..|./).length - 1;
  48. } else {
  49. return 0;
  50. }
  51. };
  52.  
  53. unmarshallSingle = function(data) {
  54. var body, chr, command, divider, headerLines, headers, i, idx, len, line, start, trim, _i, _j, _len, _ref, _ref1;
  55. divider = data.search(RegExp("" + Byte.LF + Byte.LF));
  56. headerLines = data.substring(0, divider).split(Byte.LF);
  57. command = headerLines.shift();
  58. headers = {};
  59. trim = function(str) {
  60. return str.replace(/^\s+|\s+$/g, '');
  61. };
  62. _ref = headerLines.reverse();
  63. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  64. line = _ref[_i];
  65. idx = line.indexOf(':');
  66. headers[trim(line.substring(0, idx))] = trim(line.substring(idx + 1));
  67. }
  68. body = '';
  69. start = divider + 2;
  70. if (headers['content-length']) {
  71. len = parseInt(headers['content-length']);
  72. body = ('' + data).substring(start, start + len);
  73. } else {
  74. chr = null;
  75. for (i = _j = start, _ref1 = data.length; start <= _ref1 ? _j < _ref1 : _j > _ref1; i = start <= _ref1 ? ++_j : --_j) {
  76. chr = data.charAt(i);
  77. if (chr === Byte.NULL) {
  78. break;
  79. }
  80. body += chr;
  81. }
  82. }
  83. return new Frame(command, headers, body);
  84. };
  85.  
  86. Frame.unmarshall = function(datas) {
  87. var data;
  88. return (function() {
  89. var _i, _len, _ref, _results;
  90. _ref = datas.split(RegExp("" + Byte.NULL + Byte.LF + "*"));
  91. _results = [];
  92. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  93. data = _ref[_i];
  94. if ((data != null ? data.length : void 0) > 0) {
  95. _results.push(unmarshallSingle(data));
  96. }
  97. }
  98. return _results;
  99. })();
  100. };
  101.  
  102. Frame.marshall = function(command, headers, body) {
  103. var frame;
  104. frame = new Frame(command, headers, body);
  105. return frame.toString() + Byte.NULL;
  106. };
  107.  
  108. return Frame;
  109.  
  110. })();
  111.  
  112. Client = (function() {
  113. var now;
  114.  
  115. function Client(ws) {
  116. this.ws = ws;
  117. this.ws.binaryType = "arraybuffer";
  118. this.counter = 0;
  119. this.connected = false;
  120. this.heartbeat = {
  121. outgoing: 10000,
  122. incoming: 10000
  123. };
  124. this.maxWebSocketFrameSize = 16 * 1024;
  125. this.subscriptions = {};
  126. }
  127.  
  128. Client.prototype.debug = function(message) {
  129. var _ref;
  130. return typeof window !== "undefined" && window !== null ? (_ref = window.console) != null ? _ref.log(message) : void 0 : void 0;
  131. };
  132.  
  133. now = function() {
  134. if (Date.now) {
  135. return Date.now();
  136. } else {
  137. return new Date().valueOf;
  138. }
  139. };
  140.  
  141. Client.prototype._transmit = function(command, headers, body) {
  142. var out;
  143. out = Frame.marshall(command, headers, body);
  144. if (typeof this.debug === "function") {
  145. this.debug(">>> " + out);
  146. }
  147. while (true) {
  148. if (out.length > this.maxWebSocketFrameSize) {
  149. this.ws.send(out.substring(0, this.maxWebSocketFrameSize));
  150. out = out.substring(this.maxWebSocketFrameSize);
  151. if (typeof this.debug === "function") {
  152. this.debug("remaining = " + out.length);
  153. }
  154. } else {
  155. return this.ws.send(out);
  156. }
  157. }
  158. };
  159.  
  160. Client.prototype._setupHeartbeat = function(headers) {
  161. var serverIncoming, serverOutgoing, ttl, v, _ref, _ref1,
  162. _this = this;
  163. if ((_ref = headers.version) !== Stomp.VERSIONS.V1_1 && _ref !== Stomp.VERSIONS.V1_2) {
  164. return;
  165. }
  166. _ref1 = (function() {
  167. var _i, _len, _ref1, _results;
  168. _ref1 = headers['heart-beat'].split(",");
  169. _results = [];
  170. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  171. v = _ref1[_i];
  172. _results.push(parseInt(v));
  173. }
  174. return _results;
  175. })(), serverOutgoing = _ref1[0], serverIncoming = _ref1[1];
  176. if (!(this.heartbeat.outgoing === 0 || serverIncoming === 0)) {
  177. ttl = Math.max(this.heartbeat.outgoing, serverIncoming);
  178. if (typeof this.debug === "function") {
  179. this.debug("send PING every " + ttl + "ms");
  180. }
  181. this.pinger = Stomp.setInterval(ttl, function() {
  182. _this.ws.send(Byte.LF);
  183. return typeof _this.debug === "function" ? _this.debug(">>> PING") : void 0;
  184. });
  185. }
  186. if (!(this.heartbeat.incoming === 0 || serverOutgoing === 0)) {
  187. ttl = Math.max(this.heartbeat.incoming, serverOutgoing);
  188. if (typeof this.debug === "function") {
  189. this.debug("check PONG every " + ttl + "ms");
  190. }
  191. return this.ponger = Stomp.setInterval(ttl, function() {
  192. var delta;
  193. delta = now() - _this.serverActivity;
  194. if (delta > ttl * 2) {
  195. if (typeof _this.debug === "function") {
  196. _this.debug("did not receive server activity for the last " + delta + "ms");
  197. }
  198. return _this.ws.close();
  199. }
  200. });
  201. }
  202. };
  203.  
  204. Client.prototype._parseConnect = function() {
  205. var args, connectCallback, errorCallback, headers;
  206. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  207. headers = {};
  208. switch (args.length) {
  209. case 2:
  210. headers = args[0], connectCallback = args[1];
  211. break;
  212. case 3:
  213. if (args[1] instanceof Function) {
  214. headers = args[0], connectCallback = args[1], errorCallback = args[2];
  215. } else {
  216. headers.login = args[0], headers.passcode = args[1], connectCallback = args[2];
  217. }
  218. break;
  219. case 4:
  220. headers.login = args[0], headers.passcode = args[1], connectCallback = args[2], errorCallback = args[3];
  221. break;
  222. default:
  223. headers.login = args[0], headers.passcode = args[1], connectCallback = args[2], errorCallback = args[3], headers.host = args[4];
  224. }
  225. return [headers, connectCallback, errorCallback];
  226. };
  227.  
  228. Client.prototype.connect = function() {
  229. var args, errorCallback, headers, out,
  230. _this = this;
  231. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  232. out = this._parseConnect.apply(this, args);
  233. headers = out[0], this.connectCallback = out[1], errorCallback = out[2];
  234. if (typeof this.debug === "function") {
  235. this.debug("Opening Web Socket...");
  236. }
  237. this.ws.onmessage = function(evt) {
  238. var arr, c, client, data, frame, messageID, onreceive, subscription, _i, _len, _ref, _results;
  239. data = typeof ArrayBuffer !== 'undefined' && evt.data instanceof ArrayBuffer ? (arr = new Uint8Array(evt.data), typeof _this.debug === "function" ? _this.debug("--- got data length: " + arr.length) : void 0, ((function() {
  240. var _i, _len, _results;
  241. _results = [];
  242. for (_i = 0, _len = arr.length; _i < _len; _i++) {
  243. c = arr[_i];
  244. _results.push(String.fromCharCode(c));
  245. }
  246. return _results;
  247. })()).join('')) : evt.data;
  248. _this.serverActivity = now();
  249. if (data === Byte.LF) {
  250. if (typeof _this.debug === "function") {
  251. _this.debug("<<< PONG");
  252. }
  253. return;
  254. }
  255. if (typeof _this.debug === "function") {
  256. _this.debug("<<< " + data);
  257. }
  258. _ref = Frame.unmarshall(data);
  259. _results = [];
  260. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  261. frame = _ref[_i];
  262. switch (frame.command) {
  263. case "CONNECTED":
  264. if (typeof _this.debug === "function") {
  265. _this.debug("connected to server " + frame.headers.server);
  266. }
  267. _this.connected = true;
  268. _this._setupHeartbeat(frame.headers);
  269. _results.push(typeof _this.connectCallback === "function" ? _this.connectCallback(frame) : void 0);
  270. break;
  271. case "MESSAGE":
  272. subscription = frame.headers.subscription;
  273. onreceive = _this.subscriptions[subscription] || _this.onreceive;
  274. if (onreceive) {
  275. client = _this;
  276. messageID = frame.headers["message-id"];
  277. frame.ack = function(headers) {
  278. if (headers == null) {
  279. headers = {};
  280. }
  281. return client.ack(messageID, subscription, headers);
  282. };
  283. frame.nack = function(headers) {
  284. if (headers == null) {
  285. headers = {};
  286. }
  287. return client.nack(messageID, subscription, headers);
  288. };
  289. _results.push(onreceive(frame));
  290. } else {
  291. _results.push(typeof _this.debug === "function" ? _this.debug("Unhandled received MESSAGE: " + frame) : void 0);
  292. }
  293. break;
  294. case "RECEIPT":
  295. _results.push(typeof _this.onreceipt === "function" ? _this.onreceipt(frame) : void 0);
  296. break;
  297. case "ERROR":
  298. _results.push(typeof errorCallback === "function" ? errorCallback(frame) : void 0);
  299. break;
  300. default:
  301. _results.push(typeof _this.debug === "function" ? _this.debug("Unhandled frame: " + frame) : void 0);
  302. }
  303. }
  304. return _results;
  305. };
  306. this.ws.onclose = function() {
  307. var msg;
  308. msg = "Whoops! Lost connection to " + _this.ws.url;
  309. if (typeof _this.debug === "function") {
  310. _this.debug(msg);
  311. }
  312. _this._cleanUp();
  313. return typeof errorCallback === "function" ? errorCallback(msg) : void 0;
  314. };
  315. return this.ws.onopen = function() {
  316. if (typeof _this.debug === "function") {
  317. _this.debug('Web Socket Opened...');
  318. }
  319. headers["accept-version"] = Stomp.VERSIONS.supportedVersions();
  320. headers["heart-beat"] = [_this.heartbeat.outgoing, _this.heartbeat.incoming].join(',');
  321. return _this._transmit("CONNECT", headers);
  322. };
  323. };
  324.  
  325. Client.prototype.disconnect = function(disconnectCallback) {
  326. this._transmit("DISCONNECT");
  327. this.ws.onclose = null;
  328. this.ws.close();
  329. this._cleanUp();
  330. return typeof disconnectCallback === "function" ? disconnectCallback() : void 0;
  331. };
  332.  
  333. Client.prototype._cleanUp = function() {
  334. this.connected = false;
  335. if (this.pinger) {
  336. Stomp.clearInterval(this.pinger);
  337. }
  338. if (this.ponger) {
  339. return Stomp.clearInterval(this.ponger);
  340. }
  341. };
  342.  
  343. Client.prototype.send = function(destination, headers, body) {
  344. if (headers == null) {
  345. headers = {};
  346. }
  347. if (body == null) {
  348. body = '';
  349. }
  350. headers.destination = destination;
  351. return this._transmit("SEND", headers, body);
  352. };
  353.  
  354. Client.prototype.subscribe = function(destination, callback, headers) {
  355. var client;
  356. if (headers == null) {
  357. headers = {};
  358. }
  359. if (!headers.id) {
  360. headers.id = "sub-" + this.counter++;
  361. }
  362. headers.destination = destination;
  363. this.subscriptions[headers.id] = callback;
  364. this._transmit("SUBSCRIBE", headers);
  365. client = this;
  366. return {
  367. id: headers.id,
  368. unsubscribe: function() {
  369. return client.unsubscribe(headers.id);
  370. }
  371. };
  372. };
  373.  
  374. Client.prototype.unsubscribe = function(id) {
  375. delete this.subscriptions[id];
  376. return this._transmit("UNSUBSCRIBE", {
  377. id: id
  378. });
  379. };
  380.  
  381. Client.prototype.begin = function(transaction) {
  382. var client, txid;
  383. txid = transaction || "tx-" + this.counter++;
  384. this._transmit("BEGIN", {
  385. transaction: txid
  386. });
  387. client = this;
  388. return {
  389. id: txid,
  390. commit: function() {
  391. return client.commit(txid);
  392. },
  393. abort: function() {
  394. return client.abort(txid);
  395. }
  396. };
  397. };
  398.  
  399. Client.prototype.commit = function(transaction) {
  400. return this._transmit("COMMIT", {
  401. transaction: transaction
  402. });
  403. };
  404.  
  405. Client.prototype.abort = function(transaction) {
  406. return this._transmit("ABORT", {
  407. transaction: transaction
  408. });
  409. };
  410.  
  411. Client.prototype.ack = function(messageID, subscription, headers) {
  412. if (headers == null) {
  413. headers = {};
  414. }
  415. headers["message-id"] = messageID;
  416. headers.subscription = subscription;
  417. return this._transmit("ACK", headers);
  418. };
  419.  
  420. Client.prototype.nack = function(messageID, subscription, headers) {
  421. if (headers == null) {
  422. headers = {};
  423. }
  424. headers["message-id"] = messageID;
  425. headers.subscription = subscription;
  426. return this._transmit("NACK", headers);
  427. };
  428.  
  429. return Client;
  430.  
  431. })();
  432.  
  433. Stomp = {
  434. VERSIONS: {
  435. V1_0: '1.0',
  436. V1_1: '1.1',
  437. V1_2: '1.2',
  438. supportedVersions: function() {
  439. return '1.1,1.0';
  440. }
  441. },
  442. client: function(url, protocols) {
  443. var klass, ws;
  444. if (protocols == null) {
  445. protocols = ['v10.stomp', 'v11.stomp'];
  446. }
  447. klass = Stomp.WebSocketClass || WebSocket;
  448. ws = new klass(url, protocols);
  449. return new Client(ws);
  450. },
  451. over: function(ws) {
  452. return new Client(ws);
  453. },
  454. Frame: Frame
  455. };
  456.  
  457. if (typeof window !== "undefined" && window !== null) {
  458. Stomp.setInterval = function(interval, f) {
  459. return window.setInterval(f, interval);
  460. };
  461. Stomp.clearInterval = function(id) {
  462. return window.clearInterval(id);
  463. };
  464. window.Stomp = Stomp;
  465. } else if (typeof exports !== "undefined" && exports !== null) {
  466. exports.Stomp = Stomp;
  467. } else {
  468. self.Stomp = Stomp;
  469. }
  470.  
  471. }).call(this);
RAW Paste Data