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
|
|
@ -875,7 +875,16 @@
|
|||
console.log("Connecting WebSocket to:", 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 () {
|
||||
clearTimeout(connectionTimeout);
|
||||
console.log("WebSocket connected to:", url);
|
||||
reconnectAttempts = 0;
|
||||
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");
|
||||
if (!disconnectNotified) {
|
||||
notify("Disconnected from chat server", "error");
|
||||
|
|
@ -933,6 +944,9 @@
|
|||
reconnectAttempts++;
|
||||
updateConnectionStatus("connecting");
|
||||
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