fix: use correct callback on websocket error

the error event of the websocket occurs if the connection is already closed.
Therefore, the disconnect call does nothing and the transporter won't throw the disconnected event.

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2023-03-26 11:31:05 +02:00
parent c746495e5f
commit a78ac5f097

View file

@ -5,14 +5,13 @@
*/
import { ConnectionState, MessageTransporter } from './message-transporter.js'
import { Message, MessageType } from './message.js'
import WebSocket, { CloseEvent, ErrorEvent, MessageEvent } from 'isomorphic-ws'
import WebSocket, { MessageEvent } from 'isomorphic-ws'
export class WebsocketTransporter extends MessageTransporter {
private websocket: WebSocket | undefined
private messageCallback: undefined | ((event: MessageEvent) => void)
private errorCallback: undefined | ((event: ErrorEvent) => void)
private closeCallback: undefined | ((event: CloseEvent) => void)
private closeCallback: undefined | (() => void)
constructor() {
super()
@ -41,10 +40,8 @@ export class WebsocketTransporter extends MessageTransporter {
if (this.messageCallback) {
this.websocket.removeEventListener('message', this.messageCallback)
}
if (this.errorCallback) {
this.websocket.removeEventListener('error', this.errorCallback)
}
if (this.closeCallback) {
this.websocket.removeEventListener('error', this.closeCallback)
this.websocket.removeEventListener('close', this.closeCallback)
}
}
@ -52,11 +49,10 @@ export class WebsocketTransporter extends MessageTransporter {
private bindWebsocketEvents(websocket: WebSocket) {
this.messageCallback = this.processMessageEvent.bind(this)
this.errorCallback = this.disconnect.bind(this)
this.closeCallback = this.onDisconnecting.bind(this)
websocket.addEventListener('message', this.messageCallback)
websocket.addEventListener('error', this.errorCallback)
websocket.addEventListener('error', this.closeCallback)
websocket.addEventListener('close', this.closeCallback)
}
@ -72,6 +68,11 @@ export class WebsocketTransporter extends MessageTransporter {
this.websocket?.close()
}
protected onDisconnecting() {
this.undbindEventsFromPreviousWebsocket()
super.onDisconnecting()
}
public sendMessage(content: Message<MessageType>): void {
if (this.websocket?.readyState !== WebSocket.OPEN) {
throw new Error("Can't send message over non-open socket")