Enhance WebSocket keep-alive mechanism to include bridge connections and handle stale connections
This commit is contained in:
@@ -744,6 +744,7 @@ wss.on('connection', (ws, req) => {
|
|||||||
if (url.startsWith('/ws/bridge')) {
|
if (url.startsWith('/ws/bridge')) {
|
||||||
console.log('🌉 CC:Tweaked bridge connected via WebSocket');
|
console.log('🌉 CC:Tweaked bridge connected via WebSocket');
|
||||||
bridgeClients.add(ws);
|
bridgeClients.add(ws);
|
||||||
|
ws.isAlive = true;
|
||||||
|
|
||||||
// Notify web clients that the bridge is now connected
|
// Notify web clients that the bridge is now connected
|
||||||
broadcastToClients({
|
broadcastToClients({
|
||||||
@@ -789,6 +790,8 @@ wss.on('connection', (ws, req) => {
|
|||||||
bridgeConnected: bridgeClients.size > 0,
|
bridgeConnected: bridgeClients.size > 0,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
ws.on('pong', () => { ws.isAlive = true; });
|
||||||
|
|
||||||
ws.on('error', (error) => {
|
ws.on('error', (error) => {
|
||||||
console.error('❌ Bridge WS error:', error);
|
console.error('❌ Bridge WS error:', error);
|
||||||
@@ -859,7 +862,7 @@ wss.on('connection', (ws, req) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// ========== WebSocket Keep-Alive ==========
|
// ========== 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(() => {
|
const WS_PING_INTERVAL = setInterval(() => {
|
||||||
webClients.forEach((ws) => {
|
webClients.forEach((ws) => {
|
||||||
if (!ws.isAlive) {
|
if (!ws.isAlive) {
|
||||||
@@ -869,6 +872,16 @@ const WS_PING_INTERVAL = setInterval(() => {
|
|||||||
ws.isAlive = false;
|
ws.isAlive = false;
|
||||||
ws.ping();
|
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);
|
}, 25000);
|
||||||
|
|
||||||
wss.on('close', () => {
|
wss.on('close', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user