From d181a3b5263567f47f7b68224072720044adbbf8 Mon Sep 17 00:00:00 2001 From: xangelo Date: Tue, 7 Jun 2022 00:08:46 -0400 Subject: [PATCH] track online users without async calls --- src/api.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/api.ts b/src/api.ts index 5b1edc6..86b1b93 100644 --- a/src/api.ts +++ b/src/api.ts @@ -27,6 +27,9 @@ const accountRepo = new AccountRepository(); const cityRepo = new CityRepository(); const mailRepo = new MailRepository(); +const cache: Record = { + online_users: [] +}; const msgBuffer: string[] = []; createBullBoard({ @@ -479,11 +482,12 @@ server.post<{params: {queueId: string}}, void>('/training/:queueId/cancel', asyn server.get('/server-stats', async req => { const date = new Date(); + const min = date.getMinutes(); return `
- ${(await server.ws.allSockets()).size} Online
+ ${server.ws.engine.clientsCount} Online
- Server Time: ${date.getHours()}:${date.getMinutes()} + Server Time: ${date.getHours()}:${min < 10 ? '0'+min : min}
`; }); @@ -496,6 +500,11 @@ server.ws.on('connection', async socket => { server.ws.emit('/chat-message', renderPublicChatMessage('Server', `${acct.username} logged in`)); + cache.online_users.push(acct.username); + + socket.on('disconnect', () => { + cache.online_users.splice(cache.online_users.indexOf(acct.username), 1); + }); }); server.start(); -- 2.25.1