UNPKG

@pecometer/pecots-websocket-client

Version:

PecoTS Framework Websocket Client

1 lines 3.83 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.PecoTsWebSocket=void 0;var rxjs_1=require("rxjs"),DEFAULT_TIMEOUT=3e4,DEFAULT_RECONNECTION_TRIES=10,DEFAULT_RECONNECTION_TIMEOUT=15e3,DEFAULT_PING_PONG_TIMEOUT=15e3,PecoTsWebSocket=function(){function PecoTsWebSocket(){this._listeners=[],this._endPoint="",this._socket=null,this._timeout=DEFAULT_TIMEOUT,this._pongTimeout=null,this._isConnected=!1,this._connectionID=null,this._jwtToken=null,this._connectionAttempts=0,this._config={reconnection:{count:DEFAULT_RECONNECTION_TRIES,timeout:DEFAULT_RECONNECTION_TIMEOUT}}}return Object.defineProperty(PecoTsWebSocket.prototype,"connected",{get:function(){return this._isConnected},enumerable:!1,configurable:!0}),Object.defineProperty(PecoTsWebSocket.prototype,"connectionID",{get:function(){return this._connectionID},enumerable:!1,configurable:!0}),PecoTsWebSocket.prototype.init=function(url,jwt){var _this=this;this._endPoint=url,this._jwtToken=jwt,this._socket=new WebSocket(this._endPoint),this._socket.addEventListener("error",(function(){_this._socket.close(),_this._pingTimeout&&clearTimeout(_this._pingTimeout)})),this._socket.addEventListener("close",this._closeHandler()),this._socket.addEventListener("open",(function(){_this._ping(),_this._socket.addEventListener("message",(function(message){return _this._handleMessage(message)}))}))},PecoTsWebSocket.prototype.disconnect=function(){this._socket&&(this._socket.removeEventListener("close",this._closeHandler()),this._socket.close(),this._jwtToken=null,this._socket=null)},PecoTsWebSocket.prototype.register=function(event){var _this=this;return new rxjs_1.Observable((function(observer){return _this._listeners.push({event,listener:observer}),function(){_this._listeners=_this._listeners.filter((function(e){return e.listener!==observer}))}}))},PecoTsWebSocket.prototype._ping=function(){var _this=this;this._pingTimeout&&(clearTimeout(this._pingTimeout),this._pingTimeout=null),this._pingTimeout=setTimeout((function(){_this._isConnected&&(_this._socket.send("ping"),_this._pongTimeout=setTimeout((function(){_this._isConnected=!1}),DEFAULT_PING_PONG_TIMEOUT))}),this._timeout)},PecoTsWebSocket.prototype._broadcast=function(data){for(var _a,_i=0,_b=this._listeners;_i<_b.length;_i++){var listener=_b[_i];listener.event===data.event&&listener.listener.next(null!==(_a=data.data)&&void 0!==_a?_a:null)}},PecoTsWebSocket.prototype._sendMessage=function(message){this._socket&&this._socket.send(message)},PecoTsWebSocket.prototype._closeHandler=function(){var _this=this;return function(){_this._isConnected=!1,_this._connectionID=null,_this._pingTimeout&&clearTimeout(_this._pingTimeout),_this._connectionAttempts<=_this._config.reconnection.count&&_this._jwtToken&&setTimeout((function(){_this._connectionAttempts++,_this.init(_this._endPoint,_this._jwtToken)}),_this._config.reconnection.timeout)}},PecoTsWebSocket.prototype._handleMessage=function(message){var _a,messageData=message.data;if(!1===this._isConnected&&"pong"!==messageData)this._handleInitialConnectionMessage(messageData);else if("pong"===messageData)clearTimeout(this._pongTimeout),this._pongTimeout=null,this._connectionID&&(this._isConnected=!0,this._ping());else{try{var msgJson=JSON.parse(messageData);(null===(_a=msgJson.data)||void 0===_a?void 0:_a.event)&&this._broadcast(msgJson.data)}catch(_b){}this._ping()}},PecoTsWebSocket.prototype._handleInitialConnectionMessage=function(messageData){try{var msgJson=JSON.parse(messageData);msgJson.connectionID?(this._connectionID=msgJson.connectionID,msgJson.config&&(this._config=msgJson.config),this._sendMessage(JSON.stringify({ack:"ack-response",data:this._jwtToken}))):"ack-successful"===msgJson.ack&&(this._isConnected=!0,this._connectionAttempts=0)}catch(_a){}},PecoTsWebSocket}();exports.PecoTsWebSocket=PecoTsWebSocket;