mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-24 18:56:32 -05:00
fix: disconnect websocket if trying to send over closed connection
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
57701b5d6c
commit
ef699a9123
1 changed files with 16 additions and 3 deletions
|
@ -28,7 +28,7 @@ export class WebsocketTransporter extends MessageTransporter {
|
||||||
this.websocket = websocket
|
this.websocket = websocket
|
||||||
this.bindWebsocketEvents(websocket)
|
this.bindWebsocketEvents(websocket)
|
||||||
|
|
||||||
if (this.websocket.readyState === WebSocket.OPEN) {
|
if (this.isConnected()) {
|
||||||
this.onConnected()
|
this.onConnected()
|
||||||
} else {
|
} else {
|
||||||
this.websocket.addEventListener('open', this.onConnected.bind(this))
|
this.websocket.addEventListener('open', this.onConnected.bind(this))
|
||||||
|
@ -69,13 +69,26 @@ export class WebsocketTransporter extends MessageTransporter {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected onDisconnecting() {
|
protected onDisconnecting() {
|
||||||
|
if (this.websocket === undefined) {
|
||||||
|
return
|
||||||
|
}
|
||||||
this.undbindEventsFromPreviousWebsocket()
|
this.undbindEventsFromPreviousWebsocket()
|
||||||
|
this.websocket = undefined
|
||||||
super.onDisconnecting()
|
super.onDisconnecting()
|
||||||
}
|
}
|
||||||
|
|
||||||
public sendMessage(content: Message<MessageType>): void {
|
public sendMessage(content: Message<MessageType>): void {
|
||||||
if (this.websocket?.readyState !== WebSocket.OPEN) {
|
if (!this.isConnected()) {
|
||||||
throw new Error("Can't send message over non-open socket")
|
this.onDisconnecting()
|
||||||
|
console.debug(
|
||||||
|
"Can't send message over closed connection. Triggering onDisconencted event. Message that couldn't be sent was",
|
||||||
|
content
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.websocket === undefined) {
|
||||||
|
throw new Error('websocket transporter has no websocket connection')
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in a new issue