From beef13d273c5933e43b05d4348fb58c0a49a9b2f Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Mon, 6 Sep 2021 17:11:28 +0200 Subject: [PATCH] lib/client: remove WebSocket error event handler The error event handler is useless, because the error event is never emitted without a close event, and doesn't give any details about the error. --- lib/client.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/client.js b/lib/client.js index 61b37a2..f3ddca8 100644 --- a/lib/client.js +++ b/lib/client.js @@ -22,6 +22,11 @@ const permanentCaps = [ const RECONNECT_DELAY_SEC = 10; +// WebSocket status codes +// https://www.rfc-editor.org/rfc/rfc6455.html#section-7.4.1 +const NORMAL_CLOSURE = 1000; +const GOING_AWAY = 1001; + let lastLabel = 0; export default class Client extends EventTarget { @@ -90,6 +95,10 @@ export default class Client extends EventTarget { this.ws.addEventListener("close", (event) => { console.log("Connection closed (code: " + event.code + ")"); + if (event.code !== NORMAL_CLOSURE && event.code !== GOING_AWAY) { + this.dispatchEvent(new CustomEvent("error", { detail: "Connection error" })); + } + this.ws = null; this.setStatus(Client.Status.DISCONNECTED); this.nick = null; @@ -118,10 +127,6 @@ export default class Client extends EventTarget { } } }); - - this.ws.addEventListener("error", () => { - this.dispatchEvent(new CustomEvent("error", { detail: "Connection error" })); - }); } disconnect() {