stomp.js 13 KB

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