From 414d93ebabcdd4c694e6739e19a72bc541e4c7f3 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 5 Nov 2024 13:54:49 +0100 Subject: [PATCH] client: reconnect immediately if network comes online during backoff --- lib/client.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/client.js b/lib/client.js index 90c25ba..9092ed8 100644 --- a/lib/client.js +++ b/lib/client.js @@ -201,13 +201,10 @@ export default class Client extends EventTarget { this.monitored = new irc.CaseMapMap(null, irc.CaseMapping.RFC1459); if (this.autoReconnect) { + window.addEventListener("online", this.handleOnline); + if (!navigator.onLine) { console.info("Waiting for network to go back online"); - const handleOnline = () => { - window.removeEventListener("online", handleOnline); - this.reconnect(); - }; - window.addEventListener("online", handleOnline); } else { let delay = this.reconnectBackoff.next(); console.info("Reconnecting to server in " + (delay / 1000) + " seconds"); @@ -226,6 +223,8 @@ export default class Client extends EventTarget { clearTimeout(this.reconnectTimeoutID); this.reconnectTimeoutID = null; + window.removeEventListener("online", this.handleOnline); + this.setPingInterval(0); if (this.ws) { @@ -245,6 +244,13 @@ export default class Client extends EventTarget { this.dispatchEvent(new CustomEvent("error", { detail: err })); } + handleOnline() { + window.removeEventListener("online", this.handleOnline); + if (this.autoReconnect && this.status === Client.Status.DISCONNECTED) { + this.reconnect(); + } + } + handleOpen() { console.log("Connection opened"); this.setStatus(Client.Status.REGISTERING);