From e47a5de981b5a3ce9762c2876993ba83eaafd94d Mon Sep 17 00:00:00 2001 From: wang9hu Date: Mon, 26 Feb 2024 14:06:45 -0800 Subject: [PATCH 1/2] add pong response for heartbeat request sent by client via websocket --- src/websockets/TimerService/clientsHandler.js | 23 +++++++++---------- src/websockets/index.js | 10 +++++--- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/websockets/TimerService/clientsHandler.js b/src/websockets/TimerService/clientsHandler.js index 60eb33fa4..68b6ec501 100644 --- a/src/websockets/TimerService/clientsHandler.js +++ b/src/websockets/TimerService/clientsHandler.js @@ -44,6 +44,7 @@ export const action = { FORCED_PAUSE: 'FORCED_PAUSE', ACK_FORCED: 'ACK_FORCED', START_CHIME: 'START_CHIME', + HEARTBEAT: 'ping', }; const MAX_HOURS = 5; @@ -170,22 +171,21 @@ export const handleMessage = async (msg, clients, userId) => { const client = clients.get(userId); let resp = null; - const req = msg.toString(); - switch (req) { + switch (msg) { case action.START_TIMER: startTimer(client); break; - case req.match(/SET_GOAL=/i)?.input: - setGoal(client, req); + case msg.match(/SET_GOAL=/i)?.input: + setGoal(client, msg); break; - case req.match(/ADD_TO_GOAL=/i)?.input: - addGoal(client, req); + case msg.match(/ADD_TO_GOAL=/i)?.input: + addGoal(client, msg); break; - case req.match(/REMOVE_FROM_GOAL=/i)?.input: - removeGoal(client, req); + case msg.match(/REMOVE_FROM_GOAL=/i)?.input: + removeGoal(client, msg); break; - case req.match(/START_CHIME=/i)?.input: - startChime(client, req); + case msg.match(/START_CHIME=/i)?.input: + startChime(client, msg); break; case action.PAUSE_TIMER: pauseTimer(client); @@ -202,11 +202,10 @@ export const handleMessage = async (msg, clients, userId) => { case action.STOP_TIMER: stopTimer(client); break; - default: resp = { ...client, - error: `Unknown operation ${req}, please use one of ${action}`, + error: `Unknown operation ${msg}, please use one of ${action}`, }; break; } diff --git a/src/websockets/index.js b/src/websockets/index.js index 5da85f729..a12fa18cb 100644 --- a/src/websockets/index.js +++ b/src/websockets/index.js @@ -97,7 +97,12 @@ export default async (expServer) => { * And we broadcast the response to all the clients that are connected to the same user. */ ws.on("message", async (data) => { - const resp = await handleMessage(data, clients, userId); + const msg = data.toString(); + if (msg === action.HEARTBEAT) { + ws.send(JSON.stringify({ heartbeat: "pong" })); + return; + } + const resp = await handleMessage(msg, clients, userId); broadcastToSameUser(connections, userId, resp); }); @@ -121,7 +126,6 @@ export default async (expServer) => { }); // For each new connection we start a time interval of 1min to check if the connection is alive. - // change to 1min before push const interval = setInterval(() => { wss.clients.forEach((ws) => { if (ws.isAlive === false) { @@ -130,7 +134,7 @@ export default async (expServer) => { ws.isAlive = false; ws.ping(); }); - }, 10000); + }, 60000); wss.on('close', () => { clearInterval(interval); From e8be57709269cd4e630fc2e9799435adb6d27bca Mon Sep 17 00:00:00 2001 From: wang9hu Date: Mon, 26 Feb 2024 16:08:03 -0800 Subject: [PATCH 2/2] fix timer websocket error response --- src/websockets/TimerService/clientsHandler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/websockets/TimerService/clientsHandler.js b/src/websockets/TimerService/clientsHandler.js index 68b6ec501..89c774caf 100644 --- a/src/websockets/TimerService/clientsHandler.js +++ b/src/websockets/TimerService/clientsHandler.js @@ -205,7 +205,7 @@ export const handleMessage = async (msg, clients, userId) => { default: resp = { ...client, - error: `Unknown operation ${msg}, please use one of ${action}`, + error: `Unknown operation ${msg}, please use one from { ${Object.values(action).join(', ')} }`, }; break; }