From: xangelo Date: Tue, 7 Jun 2022 04:08:46 +0000 (-0400) Subject: track online users without async calls X-Git-Url: https://git.xangelo.ca/?a=commitdiff_plain;h=d181a3b5263567f47f7b68224072720044adbbf8;p=browser-rts.git track online users without async calls --- 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();