Enhance WebSocket keep-alive mechanism to include bridge connections and handle stale connections

This commit is contained in:
MayaTheShy
2026-03-22 02:41:52 -04:00
parent 1a6d32c16b
commit 78674714b1

View File

@@ -744,6 +744,7 @@ wss.on('connection', (ws, req) => {
if (url.startsWith('/ws/bridge')) {
console.log('🌉 CC:Tweaked bridge connected via WebSocket');
bridgeClients.add(ws);
ws.isAlive = true;
// Notify web clients that the bridge is now connected
broadcastToClients({
@@ -789,6 +790,8 @@ wss.on('connection', (ws, req) => {
bridgeConnected: bridgeClients.size > 0,
});
});
ws.on('pong', () => { ws.isAlive = true; });
ws.on('error', (error) => {
console.error('❌ Bridge WS error:', error);
@@ -859,7 +862,7 @@ wss.on('connection', (ws, req) => {
});
// ========== WebSocket Keep-Alive ==========
// Ping all web clients every 25s to keep connections alive through reverse proxies
// Ping all web clients and bridge connections every 25s to keep connections alive
const WS_PING_INTERVAL = setInterval(() => {
webClients.forEach((ws) => {
if (!ws.isAlive) {
@@ -869,6 +872,16 @@ const WS_PING_INTERVAL = setInterval(() => {
ws.isAlive = false;
ws.ping();
});
bridgeClients.forEach((ws) => {
if (!ws.isAlive) {
console.log('🌉 Bridge connection stale — terminating');
bridgeClients.delete(ws);
broadcastToClients({ type: 'state_update', bridgeConnected: bridgeClients.size > 0 });
return ws.terminate();
}
ws.isAlive = false;
ws.ping();
});
}, 25000);
wss.on('close', () => {