UNPKG

ws-client-js

Version:

WebSocket browser client implementation with reconnect behavior.

2 lines (1 loc) 6.29 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=function(){function t(){}return t.prototype.serialize=function(t){return JSON.stringify(t)},t.prototype.deserialize=function(t){if("string"==typeof t)return JSON.parse(t);throw new Error("Deserialize failed for data: "+t)},t}(),e=Object.freeze({CONNECTING:"connecting",OPEN:"open",CLOSED:"closed"}),n=Object.freeze({NORMAL:1e3,GOING_AWAY:1001,PROTOCOL_ERROR:1002,UNSUPPORTED_DATA:1003,RESERVED:1004,NO_STATUS_RESERVED:1005,ABNORMAL_CLOSURE_RESERVED:1006,INVALID_FRAME_PAYLOAD_DATA:1007,POLICY_VIOLATION:1008,MESSAGE_TOO_BIG:1009,MISSING_EXTENSION:1010,INTERNAL_ERROR:1011,SERVICE_RESTART:1012,TRY_AGAIN_LATER:1013,BAD_GATEWAY:1014,TLS_HANDSHAKE_RESERVED:1015}),s=function(){return(s=Object.assign||function(t){for(var e,n=1,s=arguments.length;n<s;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t}).apply(this,arguments)},o=function(){function t(t,e){this.attemptsMax=0,this.skipCloseEventCodes=[],this.initialState={delay:t.delay,attempts:0},this.state={delay:this.initialState.delay,attempts:this.initialState.attempts},this.attemptsMax=t.attempts,this.delayIncreaseType=t.delayIncreaseType,this.skipCloseEventCodes=t.skipCloseEventCodes,this.callbacks=e,this.perform=this.perform.bind(this)}return Object.defineProperty(t.prototype,"isStarted",{get:function(){return this.state.attempts>this.initialState.attempts},enumerable:!0,configurable:!0}),t.prototype.canApply=function(t){return-1===this.skipCloseEventCodes.indexOf(t)},t.prototype.start=function(){this.state.attempts+=1,this.state.attempts>=this.attemptsMax?(this.resetState(),this.callbacks.onEnd()):(this.clearTimer(),this.timeoutId=window.setTimeout(this.perform,this.state.delay))},t.prototype.stop=function(){this.resetState(),this.clearTimer()},t.prototype.perform=function(){switch(this.delayIncreaseType){case"twice":this.state.delay=2*this.state.delay}this.callbacks.onNext()},t.prototype.resetState=function(){this.state.attempts=this.initialState.attempts,this.state.delay=this.initialState.delay},t.prototype.clearTimer=function(){this.timeoutId&&(clearTimeout(this.timeoutId),this.timeoutId=void 0)},t}(),i=function(){function t(t,n){var i;if(this.status=e.CONNECTING,this.options={url:"",debug:!1},this.connection=null,this.error=null,this.resendQueue=[],this.listeners=((i={}).onOpen=[],i.onReopen=[],i.onMessage=[],i.onClose=[],i.onError=[],i),this.serializer=t,this.options=s(s({},this.options),n),-1===this.options.url.search(/(wss?|https?)/))throw new Error("Not valid webSocket URL: "+this.options.url);this.handleMessage=this.handleMessage.bind(this),this.handleOpen=this.handleOpen.bind(this),this.handleError=this.handleError.bind(this),this.handleClose=this.handleClose.bind(this),this.handleReconnectNext=this.handleReconnectNext.bind(this),this.handleReconnectEnd=this.handleReconnectEnd.bind(this),n.reconnect&&(this.reconnect=new o(n.reconnect,{onNext:this.handleReconnectNext,onEnd:this.handleReconnectEnd}))}return t.prototype.onOpen=function(t){return this.status===e.OPEN?t():this.listeners.onOpen.push(t),this.getEventListenerDisposer("onOpen",t)},t.prototype.onReopen=function(t){return this.listeners.onReopen.push(t),this.getEventListenerDisposer("onReopen",t)},t.prototype.onMessage=function(t){return this.listeners.onMessage.push(t),this.getEventListenerDisposer("onMessage",t)},t.prototype.onClose=function(t){return this.listeners.onClose.push(t),this.getEventListenerDisposer("onClose",t)},t.prototype.onError=function(t){return this.listeners.onError.push(t),this.getEventListenerDisposer("onError",t)},t.prototype.connect=function(t){null===this.connection&&(this.protocol=t,this.status=e.CONNECTING,this.connection=new WebSocket(this.options.url,t),this.connection.onmessage=this.handleMessage,this.connection.onopen=this.handleOpen,this.connection.onerror=this.handleError,this.connection.onclose=this.handleClose)},t.prototype.disconnect=function(t){null!==this.connection&&(this.status=e.CLOSED,this.notifyListeners("onClose",t||{code:n.NORMAL,reason:"Closed by client"}),this.connection.close(),this.connection=null)},t.prototype.send=function(t){return this.status===e.CONNECTING?(this.resendQueue.push(t),!0):this.status===e.OPEN&&this.connection?(this.connection.send(this.serializer.serialize(t)),!0):(this.notifyListeners("onError"),!1)},t.prototype.resend=function(){if(0!==this.resendQueue.length&&this.connection){for(var t=0;t<this.resendQueue.length;t+=1)this.connection.send(this.serializer.serialize(this.resendQueue[t]));this.resendQueue=[]}},t.prototype.handleMessage=function(t){this.notifyListeners("onMessage",this.serializer.deserialize(t.data))},t.prototype.handleOpen=function(t){this.status=e.OPEN,this.reconnect&&this.reconnect.isStarted?(this.reconnect.stop(),this.error=null,this.notifyListeners("onReopen"),this.log("socket reopened",t)):(this.notifyListeners("onOpen"),this.log("socket opened",t)),this.resend()},t.prototype.handleError=function(t){this.notifyListeners("onError"),this.error=t,this.log("socket error",t)},t.prototype.handleReconnectNext=function(){this.status=e.CONNECTING,this.connection&&(this.connection=null),this.connect(this.protocol)},t.prototype.handleReconnectEnd=function(){this.status=e.CLOSED,this.disconnect({code:n.NORMAL,reason:"Reconnect failed"})},t.prototype.handleClose=function(t){this.reconnect&&(this.error||this.reconnect.canApply(t.code))?this.reconnect.start():(this.log("socket stopped",t),this.disconnect({code:t.code,reason:t.reason}))},t.prototype.notifyListeners=function(t,e){switch(t){case"onOpen":case"onError":case"onReopen":for(var n=0;n<this.listeners[t].length;n+=1)this.listeners[t][n]();break;case"onClose":case"onMessage":for(n=0;n<this.listeners[t].length;n+=1)this.listeners[t][n](e)}},t.prototype.getEventListenerDisposer=function(t,e){var n=this;return function(){n.listeners[t]=n.listeners[t].filter((function(t){return t!==e}))}},t.prototype.log=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];this.options.debug&&(console.group("WebSocket Connection log"),t.forEach((function(t){"object"==typeof t?console.dir(t):console.info(t)})),console.groupEnd())},t}();exports.CLOSE_EVENT_CODE=n,exports.Connection=i,exports.JsonSerializer=t,exports.STATUS=e;