From e89d83c4bb57c2068ab7931f2e26b146b875db92 Mon Sep 17 00:00:00 2001 From: Moepchi Date: Sun, 27 Sep 2026 19:05:26 +0000 Subject: [PATCH] Web: socket handlers always see current state (A6) wireSocket's handlers were created once per socket, so they kept the state from connect time: "ignore contact" never applied, the file list didn't refresh after uploads, the channel-password dialog lacked the name, log texts stayed in the old language and reconnect restarted the mic with old settings. The handlers are now rebuilt every render and the socket calls the latest version through a ref. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU --- web/src/App.tsx | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/web/src/App.tsx b/web/src/App.tsx index f1aa97f..35cf29b 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -7122,7 +7122,10 @@ function AppInner() { // session — active or parked in the background — getting a fresh socket // after an unexpected drop), so a reconnect behaves identically to the // original connect from the gateway/UI's point of view. - const wireSocket = (sessionId: string, socket: WebSocket | DemoSocket, params: ConnectParams) => { + // Rebuilt every render and only ever called through socketHandlersRef, so + // a long-lived socket always sees current state (clients, contacts, t, + // mic settings, ...) instead of whatever existed when it connected. + const socketHandlers = (sessionId: string, socket: WebSocket | DemoSocket, params: ConnectParams) => { const connectHost = params.host; const connectNickname = params.nickname; const connectServerPassword = params.serverPassword; @@ -7134,7 +7137,7 @@ function AppInner() { const connectFavoriteId = params.favoriteId; const connectIdentityBlob = identities.find((i) => i.id === connectIdentityId)?.blob ?? undefined; - socket.onopen = () => { + const onopen = () => { logClient("info", "Connection", `Connecting to ${connectHost}…`); socket.send( JSON.stringify({ @@ -7151,7 +7154,7 @@ function AppInner() { ); }; - socket.onmessage = (event) => { + const onmessage = (event: MessageEvent) => { const data = JSON.parse(event.data); // Broadcast, not tied to any one session - show it regardless of which @@ -7412,8 +7415,6 @@ function AppInner() { channelPasswordCacheRef.current.delete(data.channelId); setChannelPasswordPrompt((prev) => ({ channelId: data.channelId, - // `channels` here can be stale (this handler's closure is fixed at - // connect time) - the dialog falls back to showing just the id. channelName: channels.find((c) => c.id === data.channelId)?.name ?? "", wrongPassword: prev?.channelId === data.channelId, })); @@ -7527,7 +7528,7 @@ function AppInner() { } }; - socket.onerror = () => { + const onerror = () => { logClient("error", "WebSocket", "WebSocket error (is the gateway running?)"); if (sessionId !== activeSessionIdRef.current) { const rec = sessionsRef.current.get(sessionId); @@ -7549,7 +7550,7 @@ function AppInner() { appendLog({ text: "WebSocket error (is the gateway running?)", kind: "error" }); } }; - socket.onclose = () => { + const onclose = () => { if (sessionId !== activeSessionIdRef.current) { const rec = sessionsRef.current.get(sessionId); // Missing already means a deliberate disconnect (the "disconnected" @@ -7608,6 +7609,17 @@ function AppInner() { removeSession(sessionId, { skipSocketClose: true }); } }; + return { onopen, onmessage, onerror, onclose }; + }; + const socketHandlersRef = useRef(socketHandlers); + socketHandlersRef.current = socketHandlers; + + const wireSocket = (sessionId: string, socket: WebSocket | DemoSocket, params: ConnectParams) => { + const current = () => socketHandlersRef.current(sessionId, socket, params); + socket.onopen = () => current().onopen(); + socket.onmessage = (event) => current().onmessage(event); + socket.onerror = () => current().onerror(); + socket.onclose = () => current().onclose(); }; const handleConnect = (overrides?: {