-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathstomp.js
More file actions
8 lines (7 loc) · 8.94 KB
/
stomp.js
File metadata and controls
8 lines (7 loc) · 8.94 KB
1
2
3
4
5
6
7
8
// Generated by CoffeeScript 1.12.3
/*
Stomp Over WebSocket http://www.jmesnil.net/stomp-websocket/doc/ | Apache License V2.0
Copyright (C) 2010-2013 [Jeff Mesnil](http://jmesnil.net/)
Copyright (C) 2012 [FuseSource, Inc.](http://fusesource.com)
*/
(function(){var t,e,n,i,r={}.hasOwnProperty,o=[].slice;t={LF:"\n",NULL:"\0"};n=function(){var e;function n(t,e,n){this.command=t;this.headers=e!=null?e:{};this.body=n!=null?n:""}n.prototype.toString=function(){var e,i,o,s,u;e=[this.command];s=this.headers["content-length"]===false?true:false;if(s){delete this.headers["content-length"]}o=this.headers;for(i in o){if(!r.call(o,i))continue;u=o[i];e.push(i+":"+u)}if(this.body&&!s){e.push("content-length:"+n.sizeOfUTF8(this.body))}e.push(t.LF+this.body);return e.join(t.LF)};n.sizeOfUTF8=function(t){if(t){return encodeURI(t).match(/%..|./g).length}else{return 0}};e=function(e){var i,r,o,s,u,c,a,f,h,l,p,d,b,g,v,y,m,S;s=e.search(RegExp(""+t.LF+t.LF));u=e.substring(0,s).split(t.LF);o=u.shift();c={};S=function(t){return t.replace(/^\s+|\s+$/g,"")};g=u.reverse();for(h=0,d=g.length;h<d;h++){b=g[h];f=b.indexOf(":");c[S(b.substring(0,f))]=S(b.substring(f+1))}i="";m=s+2;if(c["content-length"]){p=parseInt(c["content-length"]);i=(""+e).substring(m,m+p)}else{r=null;for(a=l=v=m,y=e.length;v<=y?l<y:l>y;a=v<=y?++l:--l){r=e.charAt(a);if(r===t.NULL){break}i+=r}}return new n(o,c,i)};n.unmarshall=function(n){var i,r,o,s;r=n.split(RegExp(""+t.NULL+t.LF+"*"));s={frames:[],partial:""};s.frames=function(){var t,n,o,s;o=r.slice(0,-1);s=[];for(t=0,n=o.length;t<n;t++){i=o[t];s.push(e(i))}return s}();o=r.slice(-1)[0];if(o===t.LF||o.search(RegExp(""+t.NULL+t.LF+"*$"))!==-1){s.frames.push(e(o))}else{s.partial=o}return s};n.marshall=function(e,i,r){var o;o=new n(e,i,r);return o.toString()+t.NULL};return n}();e=function(){var e;function r(t){this.ws_fn=function(){var e;e=t();e.binaryType="arraybuffer";return e};this.reconnect_delay=0;this.counter=0;this.connected=false;this.heartbeat={outgoing:1e4,incoming:1e4};this.maxWebSocketFrameSize=16*1024;this.subscriptions={};this.partialData=""}r.prototype.debug=function(t){var e;return typeof window!=="undefined"&&window!==null?(e=window.console)!=null?e.log(t):void 0:void 0};e=function(){if(Date.now){return Date.now()}else{return(new Date).valueOf}};r.prototype._transmit=function(t,e,i){var r;r=n.marshall(t,e,i);if(typeof this.debug==="function"){this.debug(">>> "+r)}while(true){if(r.length>this.maxWebSocketFrameSize){this.ws.send(r.substring(0,this.maxWebSocketFrameSize));r=r.substring(this.maxWebSocketFrameSize);if(typeof this.debug==="function"){this.debug("remaining = "+r.length)}}else{return this.ws.send(r)}}};r.prototype._setupHeartbeat=function(n){var r,o,s,u,c,a;if((r=n.version)!==i.VERSIONS.V1_1&&r!==i.VERSIONS.V1_2){return}o=function(){var t,e,i,r;i=n["heart-beat"].split(",");r=[];for(t=0,e=i.length;t<e;t++){a=i[t];r.push(parseInt(a))}return r}(),u=o[0],s=o[1];if(!(this.heartbeat.outgoing===0||s===0)){c=Math.max(this.heartbeat.outgoing,s);if(typeof this.debug==="function"){this.debug("send PING every "+c+"ms")}this.pinger=i.setInterval(c,function(e){return function(){e.ws.send(t.LF);return typeof e.debug==="function"?e.debug(">>> PING"):void 0}}(this))}if(!(this.heartbeat.incoming===0||u===0)){c=Math.max(this.heartbeat.incoming,u);if(typeof this.debug==="function"){this.debug("check PONG every "+c+"ms")}return this.ponger=i.setInterval(c,function(t){return function(){var n;n=e()-t.serverActivity;if(n>c*2){if(typeof t.debug==="function"){t.debug("did not receive server activity for the last "+n+"ms")}return t.ws.close()}}}(this))}};r.prototype._parseConnect=function(){var t,e,n,i;t=1<=arguments.length?o.call(arguments,0):[];i={};switch(t.length){case 2:i=t[0],e=t[1];break;case 3:if(t[1]instanceof Function){i=t[0],e=t[1],n=t[2]}else{i.login=t[0],i.passcode=t[1],e=t[2]}break;case 4:i.login=t[0],i.passcode=t[1],e=t[2],n=t[3];break;default:i.login=t[0],i.passcode=t[1],e=t[2],n=t[3],i.host=t[4]}return[i,e,n]};r.prototype.connect=function(){var t,e;t=1<=arguments.length?o.call(arguments,0):[];e=this._parseConnect.apply(this,t);this.headers=e[0],this.connectCallback=e[1],this.errorCallback=e[2];return this._connect()};r.prototype._connect=function(){var r,o;o=this.headers;r=this.errorCallback;if(typeof this.debug==="function"){this.debug("Opening Web Socket...")}this.ws=this.ws_fn();this.ws.onmessage=function(o){return function(s){var u,c,a,f,h,l,p,d,b,g,v,y,m;f=typeof ArrayBuffer!=="undefined"&&s.data instanceof ArrayBuffer?(u=new Uint8Array(s.data),typeof o.debug==="function"?o.debug("--- got data length: "+u.length):void 0,function(){var t,e,n;n=[];for(t=0,e=u.length;t<e;t++){c=u[t];n.push(String.fromCharCode(c))}return n}().join("")):s.data;o.serverActivity=e();if(f===t.LF){if(typeof o.debug==="function"){o.debug("<<< PONG")}return}if(typeof o.debug==="function"){o.debug("<<< "+f)}m=n.unmarshall(o.partialData+f);o.partialData=m.partial;g=m.frames;v=[];for(l=0,p=g.length;l<p;l++){h=g[l];switch(h.command){case"CONNECTED":if(typeof o.debug==="function"){o.debug("connected to server "+h.headers.server)}o.connected=true;o.version=h.headers.version;o._setupHeartbeat(h.headers);v.push(typeof o.connectCallback==="function"?o.connectCallback(h):void 0);break;case"MESSAGE":y=h.headers.subscription;b=o.subscriptions[y]||o.onreceive;if(b){a=o;if(o.version===i.VERSIONS.V1_2){d=h.headers["ack"]}else{d=h.headers["message-id"]}h.ack=function(t){if(t==null){t={}}return a.ack(d,y,t)};h.nack=function(t){if(t==null){t={}}return a.nack(d,y,t)};v.push(b(h))}else{v.push(typeof o.debug==="function"?o.debug("Unhandled received MESSAGE: "+h):void 0)}break;case"RECEIPT":if(h.headers["receipt-id"]===o.closeReceipt){o.ws.onclose=null;o.ws.close();v.push(o._cleanUp())}else{v.push(typeof o.onreceipt==="function"?o.onreceipt(h):void 0)}break;case"ERROR":v.push(typeof r==="function"?r(h):void 0);break;default:v.push(typeof o.debug==="function"?o.debug("Unhandled frame: "+h):void 0)}}return v}}(this);this.ws.onclose=function(t){return function(){var e;e="Whoops! Lost connection to "+t.ws.url;if(typeof t.debug==="function"){t.debug(e)}t._cleanUp();if(typeof r==="function"){r(e)}return t._schedule_reconnect()}}(this);return this.ws.onopen=function(t){return function(){if(typeof t.debug==="function"){t.debug("Web Socket Opened...")}o["accept-version"]=i.VERSIONS.supportedVersions();o["heart-beat"]=[t.heartbeat.outgoing,t.heartbeat.incoming].join(",");return t._transmit("CONNECT",o)}}(this)};r.prototype._schedule_reconnect=function(){if(this.reconnect_delay>0){this.debug("STOMP: scheduling reconnection in "+this.reconnect_delay+"ms");return setTimeout(function(t){return function(){if(t.connected){return typeof t.debug==="function"?t.debug("STOMP: already connected"):void 0}else{if(typeof t.debug==="function"){t.debug("STOMP: attempting to reconnect")}return t._connect()}}}(this),this.reconnect_delay)}};r.prototype.disconnect=function(t,e){if(e==null){e={}}if(!e.receipt){e.receipt="close-"+this.counter++}this.closeReceipt=e.receipt;this._transmit("DISCONNECT",e);return typeof t==="function"?t():void 0};r.prototype._cleanUp=function(){this.connected=false;this.subscriptions={};this.partial="";if(this.pinger){i.clearInterval(this.pinger)}if(this.ponger){return i.clearInterval(this.ponger)}};r.prototype.send=function(t,e,n){if(e==null){e={}}if(n==null){n=""}e.destination=t;return this._transmit("SEND",e,n)};r.prototype.subscribe=function(t,e,n){var i;if(n==null){n={}}if(!n.id){n.id="sub-"+this.counter++}n.destination=t;this.subscriptions[n.id]=e;this._transmit("SUBSCRIBE",n);i=this;return{id:n.id,unsubscribe:function(t){return i.unsubscribe(n.id,t)}}};r.prototype.unsubscribe=function(t,e){if(e==null){e={}}delete this.subscriptions[t];e.id=t;return this._transmit("UNSUBSCRIBE",e)};r.prototype.begin=function(t){var e,n;n=t||"tx-"+this.counter++;this._transmit("BEGIN",{transaction:n});e=this;return{id:n,commit:function(){return e.commit(n)},abort:function(){return e.abort(n)}}};r.prototype.commit=function(t){return this._transmit("COMMIT",{transaction:t})};r.prototype.abort=function(t){return this._transmit("ABORT",{transaction:t})};r.prototype.ack=function(t,e,n){if(n==null){n={}}if(this.version===i.VERSIONS.V1_2){n["id"]=t}else{n["message-id"]=t}n.subscription=e;return this._transmit("ACK",n)};r.prototype.nack=function(t,e,n){if(n==null){n={}}if(this.version===i.VERSIONS.V1_2){n["id"]=t}else{n["message-id"]=t}n.subscription=e;return this._transmit("NACK",n)};return r}();i={VERSIONS:{V1_0:"1.0",V1_1:"1.1",V1_2:"1.2",supportedVersions:function(){return"1.2,1.1,1.0"}},client:function(t,n){var r;if(n==null){n=["v10.stomp","v11.stomp","v12.stomp"]}r=function(){var e;e=i.WebSocketClass||WebSocket;return new e(t,n)};return new e(r)},over:function(t){var n;n=typeof t==="function"?t:function(){return t};return new e(n)},Frame:n};if(typeof exports!=="undefined"&&exports!==null){exports.Stomp=i}if(typeof window!=="undefined"&&window!==null){i.setInterval=function(t,e){return window.setInterval(e,t)};i.clearInterval=function(t){return window.clearInterval(t)};window.Stomp=i}else if(!exports){self.Stomp=i}}).call(this);