fix(chat): prevent stale websocket close handler from triggering reconnect loop
This commit is contained in:
1 parent
cef002862e
commit
b6705e6dbd
2 files changed
+36
-5
No files matched your search
+17
-5
@@ -79,19 +79,28 @@ export function resolveManifestUrl() {
|
||||
}
|
||||
|
||||
function connectWebSocket(room: string) {
|
||||
if (ws) ws.close();
|
||||
// Detach the old socket's close handler before closing it. Otherwise an
|
||||
// intentional reconnect (login/logout/room switch/session refresh) would
|
||||
// have the replaced socket schedule yet another reconnect, and each new
|
||||
// connection re-pulls the room's history — filling the panel with old
|
||||
// messages on a loop.
|
||||
if (ws) {
|
||||
ws.onclose = null;
|
||||
ws.close();
|
||||
}
|
||||
|
||||
const socketUrl = new URL(resolveWebSocketBaseUrl());
|
||||
socketUrl.searchParams.set("username", username);
|
||||
socketUrl.searchParams.set("room", room);
|
||||
ws = new WebSocket(socketUrl);
|
||||
const socket = new WebSocket(socketUrl);
|
||||
ws = socket;
|
||||
|
||||
ws.onopen = () => {
|
||||
socket.onopen = () => {
|
||||
console.log(`Connected to chat room: ${room}`);
|
||||
addMessage("system", "Connected to " + room);
|
||||
};
|
||||
|
||||
ws.onmessage = (event) => {
|
||||
socket.onmessage = (event) => {
|
||||
const msg = JSON.parse(event.data);
|
||||
if (msg.type === "typing") {
|
||||
showTyping(msg.username);
|
||||
@@ -119,7 +128,10 @@ function connectWebSocket(room: string) {
|
||||
addMessage(msg.type || "public", msg.text, msg.username, msg.timestamp);
|
||||
};
|
||||
|
||||
ws.onclose = () => {
|
||||
socket.onclose = () => {
|
||||
// Ignore close events from sockets that were intentionally replaced.
|
||||
if (ws !== socket) return;
|
||||
ws = null;
|
||||
console.log("Chat disconnected. Reconnecting in 3s...");
|
||||
setTimeout(() => connectWebSocket(room), 3000);
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user