Skip to content

Commit

Permalink
Merge pull request #768 from OneCommunityGlobal/xiaow_add_heartbeat_r…
Browse files Browse the repository at this point in the history
…esponse_to_client_request

Xiaow_Add heartbeat response to client request
  • Loading branch information
one-community authored Feb 27, 2024
2 parents 07853e3 + e8be577 commit 5c54a67
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
23 changes: 11 additions & 12 deletions src/websockets/TimerService/clientsHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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 from { ${Object.values(action).join(', ')} }`,
};
break;
}
Expand Down
10 changes: 7 additions & 3 deletions src/websockets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand All @@ -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) {
Expand All @@ -130,7 +134,7 @@ export default async (expServer) => {
ws.isAlive = false;
ws.ping();
});
}, 10000);
}, 60000);

wss.on('close', () => {
clearInterval(interval);
Expand Down

0 comments on commit 5c54a67

Please sign in to comment.