From 7dcf6dcaff4d12f568465e5bd212f11b329a8f6e Mon Sep 17 00:00:00 2001 From: Akita Noek Date: Wed, 10 Jan 2024 08:51:59 -0700 Subject: [PATCH] Handle case where browser doesn't give us a proper CloseEvent when closing the socket --- src/GobanSocket.ts | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/GobanSocket.ts b/src/GobanSocket.ts index 5d27e69e..2dd0e880 100644 --- a/src/GobanSocket.ts +++ b/src/GobanSocket.ts @@ -252,18 +252,18 @@ export class GobanSocket< }); socket.addEventListener("close", (event: CloseEvent) => { - if (event.code !== 1000 && !this.manually_disconnected) { + const code = event?.code; + + if (code !== 1000 && !this.manually_disconnected) { console.warn( - `GobanSocket closed with code ${event.code}: ${closeErrorCodeToString( - event.code, - )}`, + `GobanSocket closed with code ${code}: ${closeErrorCodeToString(code)}`, ); } this.rejectPromisesInFlight(); try { - this.emit("disconnect", event.code); + this.emit("disconnect", code); } catch (e) { console.error("Error in disconnect handler", e); } @@ -272,17 +272,13 @@ export class GobanSocket< return; } - if (event.code === 1014 || event.code === 1015) { + if (code === 1014 || code === 1015) { console.error("OGS Socket closed with an unrecoverable error, not reconnecting"); this.emit( "unrecoverable_error", - event.code, - event.code === 1014 - ? "bad_gateway" - : event.code === 1015 - ? "tls_handshake" - : "unknown", - closeErrorCodeToString(event.code), + code, + code === 1014 ? "bad_gateway" : code === 1015 ? "tls_handshake" : "unknown", + closeErrorCodeToString(code), ); return; }