Skip to content

Commit

Permalink
Handle case where browser doesn't give us a proper CloseEvent when cl…
Browse files Browse the repository at this point in the history
…osing the socket
  • Loading branch information
anoek committed Jan 10, 2024
1 parent 9b5830e commit 7dcf6dc
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions src/GobanSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
}
Expand Down

0 comments on commit 7dcf6dc

Please sign in to comment.