websocket status code when disconnecting.

This commit is contained in:
Miroslav Šedivý 2022-08-17 23:05:50 +02:00
parent ac6df17036
commit 11b3738244
2 changed files with 34 additions and 7 deletions

View File

@ -30,7 +30,7 @@ export class WebsocketReconnector extends ReconnectorAbstract {
if (!this._websocket.supported) return if (!this._websocket.supported) return
if (this._websocket.connected) { if (this._websocket.connected) {
this._websocket.disconnect() this._websocket.disconnect('connection replaced')
} }
let url = this._state.url let url = this._state.url
@ -45,7 +45,7 @@ export class WebsocketReconnector extends ReconnectorAbstract {
} }
public disconnect() { public disconnect() {
this._websocket.disconnect() this._websocket.disconnect('manual disconnect')
} }
public destroy() { public destroy() {

View File

@ -8,6 +8,25 @@ export interface NekoWebSocketEvents {
message: (event: string, payload: any) => void message: (event: string, payload: any) => void
} }
const statusCodeMap = {
1000: 'Normal Closure',
1001: 'Going Away',
1002: 'Protocol Error',
1003: 'Unsupported Data',
1004: '(For future)',
1005: 'No Status Received',
1006: 'Abnormal Closure',
1007: 'Invalid frame payload data',
1008: 'Policy Violation',
1009: 'Message too big',
1010: 'Missing Extension',
1011: 'Internal Error',
1012: 'Service Restart',
1013: 'Try Again Later',
1014: 'Bad Gateway',
1015: 'TLS Handshake',
} as Record<number, string>
export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> { export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> {
private _ws?: WebSocket private _ws?: WebSocket
@ -37,7 +56,7 @@ export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> {
if (typeof this._ws !== 'undefined') { if (typeof this._ws !== 'undefined') {
this._log.debug(`previous websocket connection needs to be closed`) this._log.debug(`previous websocket connection needs to be closed`)
this.disconnect() this.disconnect('connection replaced')
} }
this._ws = new WebSocket(url) this._ws = new WebSocket(url)
@ -45,12 +64,20 @@ export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> {
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, 'close') this._ws.onclose = (e: CloseEvent) => {
let reason = 'close'
if (e.code in statusCodeMap) {
reason = statusCodeMap[e.code]
}
this.onDisconnected(reason)
}
this._ws.onerror = this.onDisconnected.bind(this, 'error') this._ws.onerror = this.onDisconnected.bind(this, 'error')
this._ws.onmessage = this.onMessage.bind(this) this._ws.onmessage = this.onMessage.bind(this)
} }
public disconnect() { public disconnect(reason: string) {
if (typeof this._ws !== 'undefined') { if (typeof this._ws !== 'undefined') {
// unmount all events // unmount all events
this._ws.onopen = () => {} this._ws.onopen = () => {}
@ -59,7 +86,7 @@ export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> {
this._ws.onmessage = () => {} this._ws.onmessage = () => {}
try { try {
this._ws.close() this._ws.close(1000, reason)
} catch {} } catch {}
this._ws = undefined this._ws = undefined
@ -94,7 +121,7 @@ export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> {
} }
private onDisconnected(reason: string) { private onDisconnected(reason: string) {
this.disconnect() this.disconnect(reason)
this._log.info(`disconnected`, { reason }) this._log.info(`disconnected`, { reason })
this.emit('disconnected', new Error(`connection ${reason}`)) this.emit('disconnected', new Error(`connection ${reason}`))