websocket-client-plus
Version:
Enhanced WebSocket client with reconnect, heartbeat and message queue support.
2 lines (1 loc) • 3.57 kB
JavaScript
var t,n=require("tslib"),i=function(){function t(){this.target=new EventTarget}return t.prototype.on=function(t,n){if("function"!=typeof n)throw new TypeError("Event handler must be a function");if(!t||""===t.trim())throw Error("Event name cannot be empty");this.target.addEventListener(t,n)},t.prototype.off=function(t,n){if(!t||""===t.trim())throw Error("Event name cannot be empty");if(n&&"function"!=typeof n)throw new TypeError("Event handler must be a function");this.target.removeEventListener(t,n||null)},t.prototype.emit=function(t,n){this.target.dispatchEvent(new CustomEvent(t,{detail:n}))},t}();!function(t){t.OPEN="OPEN",t.CLOSED="CLOSED",t.CONNECTING="CONNECTING",t.RECONNECTING="RECONNECTING",t.CLOSING="CLOSING"}(t||(t={}));var e=function(i){function e(n,e){var h,s,o,u,r,c;void 0===e&&(e={});var f=i.call(this)||this;return f.state=t.CLOSED,f.manuallyClosed=0,f.isHeartbeatWaiting=0,f.timers={heartbeat:null,reconnect:null},f.reconnectAttempts=0,f.socketUrl=n,f.baseRetryDelay=null!==(h=e.baseRetryDelay)&&void 0!==h?h:2e3,f.maxRetryDelay=null!==(s=e.maxRetryDelay)&&void 0!==s?s:6e4,f.heartbeatInterval=null!==(o=e.heartbeatInterval)&&void 0!==o?o:5e3,f.maxReconnectAttempts=null!==(u=e.maxReconnectAttempts)&&void 0!==u?u:10,f.pingMessage=null!==(r=e.pingMessage)&&void 0!==r?r:"ping",f.pongMessage=null!==(c=e.pongMessage)&&void 0!==c?c:"pong",f.currentRetryDelay=f.baseRetryDelay,f.t(),f}return n.i(e,i),e.getInstance=function(t,n){return void 0===n&&(n={}),e.h||(e.h=new e(t,n)),e.h},e.prototype.t=function(){var n=this;this.state!==t.CLOSED&&this.state!==t.RECONNECTING||(this.o(t.CONNECTING),this.manuallyClosed=0,this.ws=new WebSocket(this.socketUrl),this.ws.onopen=function(){n.u(),n.o(t.OPEN),n.l(),n.emit("channel-open")},this.ws.onmessage=function(t){var i=t.data,e=i,h=0;if("string"==typeof i)try{e=JSON.parse(i),h=1}catch(t){e=i}"string"==typeof n.pongMessage&&("string"==typeof e&&e===n.pongMessage||"object"==typeof e&&null!==e&&"type"in e&&e.type===n.pongMessage)?n.isHeartbeatWaiting=0:h&&"object"==typeof e&&null!==e&&n.emit("channel-message",e)},this.ws.onclose=function(){n.o(t.CLOSED),n.emit("channel-close"),n.manuallyClosed||n.v()},this.ws.onerror=function(t){n.emit("channel-error",t)})},e.prototype.send=function(n){this.state===t.OPEN&&this.ws&&this.ws.send(n)},e.prototype.v=function(){var n=this;this.state===t.CLOSED&&(this.reconnectAttempts>=this.maxReconnectAttempts?this.emit("reconnect-exhausted"):(this.o(t.RECONNECTING),this.p(),this.reconnectAttempts++,this.timers.reconnect=setTimeout(function(){return n.t()},this.baseRetryDelay),this.currentRetryDelay=Math.min(2*this.currentRetryDelay,this.maxRetryDelay)))},e.prototype.l=function(){var n=this;if(this.state===t.OPEN)if(this.isHeartbeatWaiting)this.m();else{try{this.ws.send(this.pingMessage),this.isHeartbeatWaiting=1}catch(t){return void this.m()}this.timers.heartbeat=setTimeout(function(){return n.l()},this.heartbeatInterval)}},e.prototype.u=function(){this.reconnectAttempts=0,this.currentRetryDelay=this.baseRetryDelay,this.isHeartbeatWaiting=0},e.prototype.p=function(){var t=this;Object.entries(this.timers).forEach(function(n){var i=n[0],e=n[1];e&&clearTimeout(e),t.timers[i]=null})},e.prototype.m=function(){this.p(),!this.ws||this.state!==t.OPEN&&this.state!==t.CONNECTING||(this.o(t.CLOSING),this.ws.close())},e.prototype.o=function(t){this.state!==t&&(this.state=t,this.emit("state-change",t))},e.prototype.destroy=function(){var t;this.manuallyClosed=1,this.p(),null===(t=this.ws)||void 0===t||t.close(),e.h=null},e.h=null,e}(i);module.exports=e;