Reconnect if connection fails

This commit is contained in:
Stefan Aebischer
2019-09-16 15:06:18 +02:00
parent 452b5f47e1
commit 4660dc98b8

View File

@@ -67,17 +67,22 @@ module.exports = class WebsocketClient extends EventEmitter {
} }
async _connect() { 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 authParams = await this._authenticate();
const url = wsUrls[this.options.livenet ? 'livenet' : 'testnet'] + authParams; 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('open', this._wsOpenHandler.bind(this));
this.ws.on('message', this._wsMessageHandler.bind(this)); this.ws.on('message', this._wsMessageHandler.bind(this));
this.ws.on('error', this._wsOnErrorHandler.bind(this)); this.ws.on('error', this._wsOnErrorHandler.bind(this));
this.ws.on('close', this._wsCloseHandler.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() { async _authenticate() {