From 983ce1e105e76c61083d08d7c3c34350734ee648 Mon Sep 17 00:00:00 2001 From: Moepchi Date: Thu, 15 Oct 2026 12:14:12 +0000 Subject: [PATCH] Rejoin the last channel after an automatic reconnect A dropped gateway connection reconnected with the original connect parameters, so everyone landed in the server's default channel again. The reconnect now aims at the channel the user was in ("/"), with its cached password if one was entered. The client log also records why a gateway connection ended: the WebSocket close code, or the keepalive watchdog firing. Until now a drop left no trace on the client side. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU --- web/src/App.tsx | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/web/src/App.tsx b/web/src/App.tsx index c4a3a36..e3be6c0 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -8000,14 +8000,17 @@ function AppInner() { // detach and run the drop handling right away. socket.onmessage = socket.onerror = socket.onclose = null; socket.close(); + logClient("warning", "Connection", `No keepalive from the gateway for ${KEEPALIVE_TIMEOUT_MS / 1000} s`); current().onclose(); }, KEEPALIVE_TIMEOUT_MS); } if (event.data !== KEEPALIVE_MESSAGE) current().onmessage(event); }; socket.onerror = () => current().onerror(); - socket.onclose = () => { + socket.onclose = (event) => { clearTimeout(watchdog); + // 1000/1001 are clean closes; 1006 means the link died without a close frame. + logClient("info", "Connection", `Gateway connection closed (code ${event.code ?? "?"})`); current().onclose(); }; }; @@ -8113,7 +8116,7 @@ function AppInner() { // network blip, or the page coming back from being backgrounded/suspended // — without disturbing which tab is active or losing chat/scroll history. const reconnectSession = (sessionId: string) => { - const params = sessionParamsRef.current.get(sessionId); + let params = sessionParamsRef.current.get(sessionId); const rec = sessionsRef.current.get(sessionId); if (!params || !rec) return; // Already open or mid-(re)connect — nothing to do. @@ -8121,6 +8124,17 @@ function AppInner() { return; } + // Come back in the channel we were in, like the native client does. Kept + // in the params so a failed attempt (empty client list) still aims there. + const isActive = sessionId === activeSessionIdRef.current; + const state = isActive ? { clients, ownClientId } : rec.parked; + const lastChannel = state?.clients.find((c) => c.id === state.ownClientId)?.channel; + if (lastChannel) { + const cachedPassword = isActive ? channelPasswordCacheRef.current.get(lastChannel) : undefined; + params = { ...params, defaultChannel: `/${lastChannel}`, channelPassword: cachedPassword ?? params.channelPassword }; + sessionParamsRef.current.set(sessionId, params); + } + logClient("info", "Connection", `Reconnecting to ${params.host}…`); const socket = DEMO_MODE ? new DemoSocket() : new WebSocket(GATEWAY_URL); rec.socket = socket;