From 4660dc98b86830f7cd0e5645c9558a0cae80f4c3 Mon Sep 17 00:00:00 2001 From: Stefan Aebischer Date: Mon, 16 Sep 2019 15:06:18 +0200 Subject: [PATCH] Reconnect if connection fails --- lib/websocket-client.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/websocket-client.js b/lib/websocket-client.js index 2e88bcf..8074306 100644 --- a/lib/websocket-client.js +++ b/lib/websocket-client.js @@ -67,17 +67,22 @@ module.exports = class WebsocketClient extends EventEmitter { } async _connect() { - if(this.readyState === READY_STATE_INITIAL) this.readyState = READY_STATE_CONNECTING; + try { + if(this.readyState === READY_STATE_INITIAL) this.readyState = READY_STATE_CONNECTING; - const authParams = await this._authenticate(); - const url = wsUrls[this.options.livenet ? 'livenet' : 'testnet'] + authParams; + const authParams = await this._authenticate(); + const url = wsUrls[this.options.livenet ? 'livenet' : 'testnet'] + authParams; - this.ws = new WebSocket(url); + this.ws = new WebSocket(url); - this.ws.on('open', this._wsOpenHandler.bind(this)); - this.ws.on('message', this._wsMessageHandler.bind(this)); - this.ws.on('error', this._wsOnErrorHandler.bind(this)); - this.ws.on('close', this._wsCloseHandler.bind(this)); + this.ws.on('open', this._wsOpenHandler.bind(this)); + this.ws.on('message', this._wsMessageHandler.bind(this)); + this.ws.on('error', this._wsOnErrorHandler.bind(this)); + this.ws.on('close', this._wsCloseHandler.bind(this)); + } catch(err) { + this.logger.error('Connection failed', err); + this._reconnect(this.options.reconnectTimeout); + } } async _authenticate() {