From e36990c368d991e7cd52be874ad200fc3bc7205a Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 8 Sep 2022 05:04:13 -0400 Subject: socket last seen --- lib/sockets.js | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'lib') diff --git a/lib/sockets.js b/lib/sockets.js index 89278580..ee16b962 100644 --- a/lib/sockets.js +++ b/lib/sockets.js @@ -59,6 +59,12 @@ export const broadcast = (type, data) => { }); }; +function subtractSeconds(numOfSeconds, date = new Date()) { + date.setSeconds(date.getSeconds() - numOfSeconds); + + return date; +} + app.register(fastifyWs); app.register(async function(app) { app.get("/ws", { websocket: true }, ({ socket: ws }, req) => { @@ -85,6 +91,7 @@ app.register(async function(app) { case "login": let token = data; + if (lastSeen[token] > subtractSeconds(5)) ws.close(); lastSeen[token] = new Date(); try { -- cgit v1.2.3