1. Keep session alive with ping-pong. 2. Refreshed tests.
This commit is contained in:
@@ -130,6 +130,13 @@ export const createWebSocketServer = (server: any) => {
|
||||
|
||||
console.log(`Client connecting to session: ${sessionId}`);
|
||||
|
||||
// Set up a ping interval to keep the connection alive
|
||||
const pingInterval = setInterval(() => {
|
||||
if (ws.readyState === WebSocket.OPEN) {
|
||||
ws.ping();
|
||||
}
|
||||
}, 30000); // Send ping every 30 seconds
|
||||
|
||||
ws.on('message', async (message) => {
|
||||
const parsedMessage = JSON.parse(message.toString());
|
||||
const { type, clientId, payload } = parsedMessage;
|
||||
@@ -151,6 +158,7 @@ export const createWebSocketServer = (server: any) => {
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
clearInterval(pingInterval); // Clear the interval when the connection closes
|
||||
let disconnectedClientId: string | null = null;
|
||||
for (const [clientId, clientWs] of sessionData.clients.entries()) {
|
||||
if (clientWs === ws) {
|
||||
@@ -205,6 +213,13 @@ export const handleWebSocketMessage = async (ws: WebSocket, sessionId: string, p
|
||||
console.log(`Client ${clientId} registered successfully for session ${sessionId}.`);
|
||||
break;
|
||||
|
||||
case 'PING':
|
||||
// Respond to client pings with a pong
|
||||
if (ws.readyState === WebSocket.OPEN) {
|
||||
ws.pong();
|
||||
}
|
||||
break;
|
||||
|
||||
case 'SETUP_SESSION':
|
||||
if (sessionData.state === SessionState.SETUP) {
|
||||
const { expectedResponses, topic, description } = payload;
|
||||
|
||||
Reference in New Issue
Block a user