From 02ba4be438791ce71436c7521dbe7d3eaf475b69 Mon Sep 17 00:00:00 2001
From: Simon Ser <contact@emersion.fr>
Date: Fri, 11 Jun 2021 09:36:11 +0200
Subject: [PATCH] Don't try to reconnect if the network is down

---
 lib/client.js | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/lib/client.js b/lib/client.js
index 8b0eaaf..565bb8c 100644
--- a/lib/client.js
+++ b/lib/client.js
@@ -69,6 +69,7 @@ export default class Client extends EventTarget {
 		this.disconnect();
 		this.autoReconnect = autoReconnect;
 
+		console.log("Connecting to " + this.params.url);
 		this.setStatus(Client.Status.CONNECTING);
 
 		try {
@@ -98,11 +99,20 @@ export default class Client extends EventTarget {
 			this.isupport = new Map();
 
 			if (this.autoReconnect) {
-				console.info("Reconnecting to server in " + RECONNECT_DELAY_SEC + " seconds");
-				clearTimeout(this.reconnectTimeoutID);
-				this.reconnectTimeoutID = setTimeout(() => {
-					this.reconnect();
-				}, RECONNECT_DELAY_SEC * 1000);
+				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 {
+					console.info("Reconnecting to server in " + RECONNECT_DELAY_SEC + " seconds");
+					clearTimeout(this.reconnectTimeoutID);
+					this.reconnectTimeoutID = setTimeout(() => {
+						this.reconnect();
+					}, RECONNECT_DELAY_SEC * 1000);
+				}
 			}
 		});