diff --git a/public/channelSync.js b/public/channelSync.js index a714ced..c8b0b76 100644 --- a/public/channelSync.js +++ b/public/channelSync.js @@ -193,6 +193,7 @@ const oldWs = M.ws; M.ws = null; oldWs.onclose = null; + oldWs.onerror = null; oldWs.close(); } M.currentChannelId = id; @@ -200,6 +201,9 @@ const proto = location.protocol === "https:" ? "wss:" : "ws:"; M.ws = new WebSocket(proto + "//" + location.host + "/api/channels/" + id + "/ws"); + // Track if we've ever connected successfully + let wasConnected = false; + M.ws.onmessage = (e) => { const data = JSON.parse(e.data); // Handle channel list updates @@ -225,6 +229,7 @@ const oldWs = M.ws; M.ws = null; oldWs.onclose = null; + oldWs.onerror = null; oldWs.close(); } M.updateUI(); @@ -279,18 +284,25 @@ M.handleUpdate(data); }; + M.ws.onerror = () => { + console.log("[WS] Connection error"); + }; + M.ws.onclose = () => { M.synced = false; M.ws = null; M.$("#sync-indicator").classList.add("disconnected"); M.updateUI(); // Auto-reconnect if user wants to be synced + // Use faster retry (2s) if never connected, slower (3s) if disconnected after connecting if (M.wantSync) { - setTimeout(() => M.connectChannel(id), 3000); + const delay = wasConnected ? 3000 : 2000; + setTimeout(() => M.connectChannel(id), delay); } }; M.ws.onopen = () => { + wasConnected = true; M.synced = true; M.$("#sync-indicator").classList.remove("disconnected"); M.updateUI(); diff --git a/public/index.html b/public/index.html index 8c40c89..dfd5a70 100644 --- a/public/index.html +++ b/public/index.html @@ -56,20 +56,33 @@