forked from jmesnil/stomp-websocket
/
stomp.min.js
2 lines (2 loc) · 6 KB
/
stomp.min.js
1
2
/*! Stomp Over WebSocket v2.0.0-next http://www.jmesnil.net/stomp-websocket/doc/ | Apache License V2.0 */
(function(){var e,t,n,r,i={}.hasOwnProperty;e={LF:"\n",NULL:"\0"},n=function(){function t(e,t,n){this.command=e,this.headers=t!=null?t:{},this.body=n!=null?n:""}return t.prototype.toString=function(){var t,n,r,s;t=[this.command],s=this.headers;for(n in s){if(!i.call(s,n))continue;r=s[n],t.push(""+n+":"+r)}return this.body&&t.push("content-length:"+(""+this.body).length),t.push(e.LF+this.body),t.join(e.LF)},t._unmarshallSingle=function(n){var r,i,s,o,u,a,f,l,c,h,p,d,v,m,g,y;o=n.search(RegExp(""+e.LF+e.LF)),u=n.substring(0,o).split(e.LF),s=u.shift(),a={},d=function(e){return e.replace(/^\s+|\s+$/g,"")},h=l=null;for(f=v=0,g=u.length;0<=g?v<g:v>g;f=0<=g?++v:--v)h=u[f],l=h.indexOf(":"),a[d(h.substring(0,l))]=d(h.substring(l+1));r="",p=o+2;if(a["content-length"])c=parseInt(a["content-length"]),r=(""+n).substring(p,p+c);else{i=null;for(f=m=p,y=n.length;p<=y?m<y:m>y;f=p<=y?++m:--m){i=n.charAt(f);if(i===e.NULL)break;r+=i}}return new t(s,a,r)},t.unmarshall=function(n){var r;return function(){var i,s,o,u;o=n.split(RegExp(""+e.NULL+e.LF+"*")),u=[];for(i=0,s=o.length;i<s;i++)r=o[i],(r!=null?r.length:void 0)>0&&u.push(t._unmarshallSingle(r));return u}()},t.marshall=function(n,r,i){var s;return s=new t(n,r,i),s.toString()+e.NULL},t}(),t=function(){function t(e){this.ws=e,this.ws.binaryType="arraybuffer",this.counter=0,this.connected=!1,this.heartbeat={outgoing:1e4,incoming:1e4},this.subscriptions={}}return t.prototype._transmit=function(e,t,r){var i;return i=n.marshall(e,t,r),typeof this.debug=="function"&&this.debug(">>> "+i),this.ws.send(i)},t.prototype._setupHeartbeat=function(t){var n,i,s,o,u,a,f=this;if((u=t.version)!==r.VERSIONS.V1_1&&u!==r.VERSIONS.V1_2)return;a=function(){var e,n,r,i;r=t["heart-beat"].split(","),i=[];for(e=0,n=r.length;e<n;e++)o=r[e],i.push(parseInt(o));return i}(),i=a[0],n=a[1],this.heartbeat.outgoing!==0&&n!==0&&(s=Math.max(this.heartbeat.outgoing,n),typeof this.debug=="function"&&this.debug("send PING every "+s+"ms"),this.pinger=typeof window!="undefined"&&window!==null?window.setInterval(function(){return f.ws.send(e.LF),typeof f.debug=="function"?f.debug(">>> PING"):void 0},s):void 0);if(this.heartbeat.incoming!==0&&i!==0)return s=Math.max(this.heartbeat.incoming,i),typeof this.debug=="function"&&this.debug("check PONG every "+s+"ms"),this.ponger=typeof window!="undefined"&&window!==null?window.setInterval(function(){var e;e=Date.now()-f.serverActivity;if(e>s*2)return typeof f.debug=="function"&&f.debug("did not receive server activity for the last "+e+"ms"),f.ws.close()},s):void 0},t.prototype.connect=function(t,i,s,o,u){var a=this;return this.connectCallback=s,typeof this.debug=="function"&&this.debug("Opening Web Socket..."),this.ws.onmessage=function(t){var r,i,s,u,f,l,c,h,p;s=typeof ArrayBuffer!="undefined"&&t.data instanceof ArrayBuffer?(r=new Uint8Array(t.data),typeof a.debug=="function"?a.debug("--- got data length: "+r.length):void 0,function(){var e,t,n;n=[];for(e=0,t=r.length;e<t;e++)i=r[e],n.push(String.fromCharCode(i));return n}().join("")):t.data,a.serverActivity=Date.now();if(s===e.LF){typeof a.debug=="function"&&a.debug("<<< PONG");return}typeof a.debug=="function"&&a.debug("<<< "+s),h=n.unmarshall(s),p=[];for(l=0,c=h.length;l<c;l++){u=h[l];switch(u.command){case"CONNECTED":typeof a.debug=="function"&&a.debug("connected to server "+u.headers.server),a.connected=!0,a._setupHeartbeat(u.headers),p.push(typeof a.connectCallback=="function"?a.connectCallback(u):void 0);break;case"MESSAGE":f=a.subscriptions[u.headers.subscription],p.push(typeof f=="function"?f(u):void 0);break;case"RECEIPT":p.push(typeof a.onreceipt=="function"?a.onreceipt(u):void 0);break;case"ERROR":p.push(typeof o=="function"?o(u):void 0);break;default:p.push(typeof a.debug=="function"?a.debug("Unhandled frame: "+u):void 0)}}return p},this.ws.onclose=function(){var e;return e="Whoops! Lost connection to "+a.ws.url,typeof a.debug=="function"&&a.debug(e),a._cleanUp(),typeof o=="function"?o(e):void 0},this.ws.onopen=function(){var e;return typeof a.debug=="function"&&a.debug("Web Socket Opened..."),e={"accept-version":r.VERSIONS.supportedVersions(),"heart-beat":[a.heartbeat.outgoing,a.heartbeat.incoming].join(",")},u&&(e.host=u),t&&(e.login=t),i&&(e.passcode=i),a._transmit("CONNECT",e)}},t.prototype.disconnect=function(e){return this._transmit("DISCONNECT"),this.ws.onclose=null,this.ws.close(),this._cleanUp(),typeof e=="function"?e():void 0},t.prototype._cleanUp=function(){this.connected=!1,this.pinger&&typeof window!="undefined"&&window!==null&&window.clearInterval(this.pinger);if(this.ponger)return typeof window!="undefined"&&window!==null?window.clearInterval(this.ponger):void 0},t.prototype.send=function(e,t,n){return t==null&&(t={}),n==null&&(n=""),t.destination=e,this._transmit("SEND",t,n)},t.prototype.subscribe=function(e,t,n){return n==null&&(n={}),n.id||(n.id="sub-"+this.counter++),n.destination=e,this.subscriptions[n.id]=t,this._transmit("SUBSCRIBE",n),n.id},t.prototype.unsubscribe=function(e){return delete this.subscriptions[e],this._transmit("UNSUBSCRIBE",{id:e})},t.prototype.begin=function(e){return this._transmit("BEGIN",{transaction:e})},t.prototype.commit=function(e){return this._transmit("COMMIT",{transaction:e})},t.prototype.abort=function(e){return this._transmit("ABORT",{transaction:e})},t.prototype.ack=function(e,t,n){return n==null&&(n={}),n["message-id"]=e,n.subscription=t,this._transmit("ACK",n)},t.prototype.nack=function(e,t,n){return n==null&&(n={}),n["message-id"]=e,n.subscription=t,this._transmit("NACK",n)},t}(),r={libVersion:"2.0.0-next",VERSIONS:{V1_0:"1.0",V1_1:"1.1",V1_2:"1.2",supportedVersions:function(){return"1.1,1.0"}},client:function(e,n){var i,s;return n==null&&(n=["v10.stomp","v11.stomp"]),i=r.WebSocketClass||WebSocket,s=new i(e,n),new t(s)},over:function(e){return new t(e)},Frame:n},typeof window!="undefined"&&window!==null?window.Stomp=r:typeof exports!="undefined"&&exports!==null?(exports.Stomp=r,r.WebSocketClass=require("./test/server.mock.js").StompServerMock):self.Stomp=r}).call(this);