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 <hi@alyssa.is>
This commit is contained in:
embr 2024-10-23 01:29:46 +02:00 committed by GitHub
parent 45392aa5ad
commit 2015a566cb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -56,7 +56,7 @@ export default class WebsocketService {
const ws = new WebSocket(url.toString()); const ws = new WebSocket(url.toString());
ws.onopen = this.onOpen.bind(this); ws.onopen = this.onOpen.bind(this);
ws.onerror = this.onError.bind(this); ws.onclose = this.onClose.bind(this);
ws.onmessage = this.onMessage.bind(this); ws.onmessage = this.onMessage.bind(this);
this.websocket = ws; this.websocket = ws;
@ -70,12 +70,10 @@ export default class WebsocketService {
this.backOff = 0; this.backOff = 0;
} }
// On ws error just close the socket and let it re-connect again for now. onClose() {
onError() {
handleNetworkingError();
this.socketDisconnected();
this.websocket.close();
if (!this.isShutdown) { if (!this.isShutdown) {
handleNetworkingError();
this.socketDisconnected();
this.scheduleReconnect(); this.scheduleReconnect();
} }
} }