fix: improve WebSocket reconnection logic and add debugging
- Add connection timeout (5s) to detect silent failures - Log WebSocket close events with code and reason - Prevent infinite reconnection loops after max attempts - Clear connection timeout when WebSocket opens or closes - Show user-friendly error after max reconnection attempts This helps diagnose why WebSocket connections are failing and prevents the infinite reconnection loop issue. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
8bfe97e92e
commit
bfc8f4da77
1 changed files with 16 additions and 2 deletions
|
|
@ -871,11 +871,20 @@
|
||||||
currentUserId +
|
currentUserId +
|
||||||
"&bot_name=" +
|
"&bot_name=" +
|
||||||
currentBotName;
|
currentBotName;
|
||||||
|
|
||||||
console.log("Connecting WebSocket to:", url);
|
console.log("Connecting WebSocket to:", url);
|
||||||
ws = new WebSocket(url);
|
ws = new WebSocket(url);
|
||||||
|
|
||||||
|
// Add connection timeout to detect silent failures
|
||||||
|
var connectionTimeout = setTimeout(function() {
|
||||||
|
if (ws.readyState !== WebSocket.OPEN) {
|
||||||
|
console.error("WebSocket connection timeout");
|
||||||
|
ws.close();
|
||||||
|
}
|
||||||
|
}, 5000);
|
||||||
|
|
||||||
ws.onopen = function () {
|
ws.onopen = function () {
|
||||||
|
clearTimeout(connectionTimeout);
|
||||||
console.log("WebSocket connected to:", url);
|
console.log("WebSocket connected to:", url);
|
||||||
reconnectAttempts = 0;
|
reconnectAttempts = 0;
|
||||||
disconnectNotified = false;
|
disconnectNotified = false;
|
||||||
|
|
@ -923,7 +932,9 @@
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
ws.onclose = function () {
|
ws.onclose = function (event) {
|
||||||
|
clearTimeout(connectionTimeout);
|
||||||
|
console.log("WebSocket closed:", event.code, event.reason);
|
||||||
updateConnectionStatus("disconnected");
|
updateConnectionStatus("disconnected");
|
||||||
if (!disconnectNotified) {
|
if (!disconnectNotified) {
|
||||||
notify("Disconnected from chat server", "error");
|
notify("Disconnected from chat server", "error");
|
||||||
|
|
@ -933,6 +944,9 @@
|
||||||
reconnectAttempts++;
|
reconnectAttempts++;
|
||||||
updateConnectionStatus("connecting");
|
updateConnectionStatus("connecting");
|
||||||
setTimeout(connectWebSocket, 1000 * reconnectAttempts);
|
setTimeout(connectWebSocket, 1000 * reconnectAttempts);
|
||||||
|
} else {
|
||||||
|
console.error("Max reconnection attempts reached. Stopping reconnection.");
|
||||||
|
notify("Could not reconnect to chat server after multiple attempts", "error");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue