From b6d61c0748a74c4c6d005cdeec1494950af75f2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Mon, 8 Feb 2021 18:27:37 +0100 Subject: [PATCH] websocket improve disconnection handling. --- src/component/internal/websocket.ts | 36 +++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/component/internal/websocket.ts b/src/component/internal/websocket.ts index fdac1ae0..0b1f7cc5 100644 --- a/src/component/internal/websocket.ts +++ b/src/component/internal/websocket.ts @@ -42,27 +42,40 @@ export class NekoWebSocket extends EventEmitter { throw new Error('attempting to create websocket while connection open') } + if (typeof this._ws !== 'undefined') { + this._log.debug(`previous websocket connection needs to be closed`) + this.disconnect(new Error('connection replaced')) + } + this.emit('connecting') this._ws = new WebSocket(this._url) this._log.info(`connecting`) this._ws.onopen = this.onConnected.bind(this) - this._ws.onclose = this.onDisconnected.bind(this, new Error('websocket closed')) - this._ws.onerror = this.onDisconnected.bind(this, new Error('websocket error')) + this._ws.onclose = this.onClose.bind(this) + this._ws.onerror = this.onError.bind(this) this._ws.onmessage = this.onMessage.bind(this) this._timeout = setTimeout(this.onTimeout.bind(this), timeout) } - public disconnect() { + public disconnect(reason?: Error) { + this.emit('disconnected', reason) + if (this._timeout) { clearTimeout(this._timeout) } - if (this.connected) { + if (typeof this._ws !== 'undefined') { + // unmount all events + this._ws.onopen = () => {} + this._ws.onclose = () => {} + this._ws.onerror = () => {} + this._ws.onmessage = () => {} + try { - this._ws!.close() + this._ws.close() } catch (err) {} this._ws = undefined @@ -102,13 +115,16 @@ export class NekoWebSocket extends EventEmitter { private onTimeout() { this._log.info(`connection timeout`) - this.onDisconnected(new Error('connection timeout')) + this.disconnect(new Error('connection timeout')) } - private onDisconnected(reason?: Error) { - this._log.info(`disconnected:`, reason?.message) + private onError() { + this._log.info(`connection error`) + this.disconnect(new Error('connection error')) + } - this.disconnect() - this.emit('disconnected', reason) + private onClose() { + this._log.info(`connection closed`) + this.disconnect(new Error('connection closed')) } }