websocket improve disconnection handling.

This commit is contained in:
Miroslav Šedivý 2021-02-08 18:27:37 +01:00
parent f2a9a33cb0
commit b6d61c0748

View File

@ -42,27 +42,40 @@ export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> {
throw new Error('attempting to create websocket while connection open') 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.emit('connecting')
this._ws = new WebSocket(this._url) this._ws = new WebSocket(this._url)
this._log.info(`connecting`) this._log.info(`connecting`)
this._ws.onopen = this.onConnected.bind(this) this._ws.onopen = this.onConnected.bind(this)
this._ws.onclose = this.onDisconnected.bind(this, new Error('websocket closed')) this._ws.onclose = this.onClose.bind(this)
this._ws.onerror = this.onDisconnected.bind(this, new Error('websocket error')) this._ws.onerror = this.onError.bind(this)
this._ws.onmessage = this.onMessage.bind(this) this._ws.onmessage = this.onMessage.bind(this)
this._timeout = setTimeout(this.onTimeout.bind(this), timeout) this._timeout = setTimeout(this.onTimeout.bind(this), timeout)
} }
public disconnect() { public disconnect(reason?: Error) {
this.emit('disconnected', reason)
if (this._timeout) { if (this._timeout) {
clearTimeout(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 { try {
this._ws!.close() this._ws.close()
} catch (err) {} } catch (err) {}
this._ws = undefined this._ws = undefined
@ -102,13 +115,16 @@ export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> {
private onTimeout() { private onTimeout() {
this._log.info(`connection timeout`) this._log.info(`connection timeout`)
this.onDisconnected(new Error('connection timeout')) this.disconnect(new Error('connection timeout'))
} }
private onDisconnected(reason?: Error) { private onError() {
this._log.info(`disconnected:`, reason?.message) this._log.info(`connection error`)
this.disconnect(new Error('connection error'))
}
this.disconnect() private onClose() {
this.emit('disconnected', reason) this._log.info(`connection closed`)
this.disconnect(new Error('connection closed'))
} }
} }