forked from daviddoran/typescript-reconnecting-websocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreconnecting-websocket.min.js
2 lines (2 loc) · 1.93 KB
/
reconnecting-websocket.min.js
1
2
var ReconnectingWebSocket=function(){function a(a,b){"undefined"==typeof b&&(b=[]),this.debug=!1,this.reconnectInterval=1e3,this.timeoutInterval=2e3,this.forcedClose=!1,this.timedOut=!1,this.protocols=[],this.onopen=function(){},this.onclose=function(){},this.onconnecting=function(){},this.onmessage=function(){},this.onerror=function(){},this.url=a,this.protocols=b,this.readyState=WebSocket.CONNECTING,this.connect(!1)}return a.prototype.connect=function(a){var b=this;this.ws=new WebSocket(this.url,this.protocols),this.onconnecting(),this.log("ReconnectingWebSocket","attempt-connect",this.url);var c=this.ws,d=setTimeout(function(){b.log("ReconnectingWebSocket","connection-timeout",b.url),b.timedOut=!0,c.close(),b.timedOut=!1},this.timeoutInterval);this.ws.onopen=function(c){clearTimeout(d),b.log("ReconnectingWebSocket","onopen",b.url),b.readyState=WebSocket.OPEN,a=!1,b.onopen(c)},this.ws.onclose=function(c){clearTimeout(d),b.ws=null,b.forcedClose?(b.readyState=WebSocket.CLOSED,b.onclose(c)):(b.readyState=WebSocket.CONNECTING,b.onconnecting(),a||b.timedOut||(b.log("ReconnectingWebSocket","onclose",b.url),b.onclose(c)),setTimeout(function(){b.connect(!0)},b.reconnectInterval))},this.ws.onmessage=function(a){b.log("ReconnectingWebSocket","onmessage",b.url,a.data),b.onmessage(a)},this.ws.onerror=function(a){b.log("ReconnectingWebSocket","onerror",b.url,a),b.onerror(a)}},a.prototype.send=function(a){if(this.ws)return this.log("ReconnectingWebSocket","send",this.url,a),this.ws.send(a);throw"INVALID_STATE_ERR : Pausing to reconnect websocket"},a.prototype.close=function(){return this.ws?(this.forcedClose=!0,this.ws.close(),!0):!1},a.prototype.refresh=function(){return this.ws?(this.ws.close(),!0):!1},a.prototype.log=function(){for(var b=[],c=0;c<arguments.length-0;c++)b[c]=arguments[c+0];(this.debug||a.debugAll)&&console.debug.apply(console,b)},a.debugAll=!1,a}();module.exports=ReconnectingWebSocket;
//# sourceMappingURL=reconnecting-websocket.js.map