1. Keep session alive with ping-pong. 2. Refreshed tests.

This commit is contained in:
AG
2025-10-16 10:48:11 +03:00
parent 6f64b1daca
commit 95684a34f7
27 changed files with 420 additions and 100 deletions

View File

@@ -92,6 +92,12 @@ const createWebSocketServer = (server) => {
}
const sessionData = exports.sessions.get(sessionId);
console.log(`Client connecting to session: ${sessionId}`);
// Set up a ping interval to keep the connection alive
const pingInterval = setInterval(() => {
if (ws.readyState === ws_1.WebSocket.OPEN) {
ws.ping();
}
}, 30000); // Send ping every 30 seconds
ws.on('message', (message) => __awaiter(void 0, void 0, void 0, function* () {
const parsedMessage = JSON.parse(message.toString());
const { type, clientId, payload } = parsedMessage;
@@ -109,6 +115,7 @@ const createWebSocketServer = (server) => {
yield (0, exports.handleWebSocketMessage)(ws, sessionId, parsedMessage);
}));
ws.on('close', () => {
clearInterval(pingInterval); // Clear the interval when the connection closes
let disconnectedClientId = null;
for (const [clientId, clientWs] of sessionData.clients.entries()) {
if (clientWs === ws) {
@@ -154,6 +161,12 @@ const handleWebSocketMessage = (ws, sessionId, parsedMessage) => __awaiter(void
case 'REGISTER_CLIENT':
console.log(`Client ${clientId} registered successfully for session ${sessionId}.`);
break;
case 'PING':
// Respond to client pings with a pong
if (ws.readyState === ws_1.WebSocket.OPEN) {
ws.pong();
}
break;
case 'SETUP_SESSION':
if (sessionData.state === SessionState.SETUP) {
const { expectedResponses, topic, description } = payload;