From 2015a566cba23d50353f97fb4f15aaf5d5752ea5 Mon Sep 17 00:00:00 2001 From: embr Date: Wed, 23 Oct 2024 01:29:46 +0200 Subject: [PATCH] Fix websocket reconnection (#3959) The `onerror` event is only triggered if the buffer is full while the socket is closed, while the `onclose` event is called for any kind of disconnection: https://websockets.spec.whatwg.org/#closeWebSocket Fixes: https://github.com/owncast/owncast/issues/3958 Co-authored-by: Alyssa Ross --- web/services/websocket-service.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/web/services/websocket-service.ts b/web/services/websocket-service.ts index e2d73842f..105f8a2b6 100644 --- a/web/services/websocket-service.ts +++ b/web/services/websocket-service.ts @@ -56,7 +56,7 @@ export default class WebsocketService { const ws = new WebSocket(url.toString()); ws.onopen = this.onOpen.bind(this); - ws.onerror = this.onError.bind(this); + ws.onclose = this.onClose.bind(this); ws.onmessage = this.onMessage.bind(this); this.websocket = ws; @@ -70,12 +70,10 @@ export default class WebsocketService { this.backOff = 0; } - // On ws error just close the socket and let it re-connect again for now. - onError() { - handleNetworkingError(); - this.socketDisconnected(); - this.websocket.close(); + onClose() { if (!this.isShutdown) { + handleNetworkingError(); + this.socketDisconnected(); this.scheduleReconnect(); } }