From 4bfcb9b35b4e9482338bfb2daa3e89adddf182fa Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 30 Mar 2020 13:41:20 +0200 Subject: [PATCH] Maintain a list of active connections --- src/controllers/UserWebSocketController.ts | 31 +++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/controllers/UserWebSocketController.ts b/src/controllers/UserWebSocketController.ts index 6e636d8..0ef3685 100644 --- a/src/controllers/UserWebSocketController.ts +++ b/src/controllers/UserWebSocketController.ts @@ -11,10 +11,17 @@ import { randomStr } from '../utils/CryptUtils'; interface PendingRequests { time: number, + clientID: number, userID: number, token: string } +interface ActiveClient { + clientID: number, + userID: number, + ws: ws +} + // Tokens are valid only 10 seconds after they are generated const TOKENS_DURATION = 10 const TOKEN_LENGTH = 20 @@ -26,6 +33,11 @@ export class UserWebSocketController { */ static pending_list: PendingRequests[] = [] + /** + * The list of active clients + */ + static active_clients: ActiveClient[] = [] + /** * Clean the list of tokens */ @@ -49,6 +61,7 @@ export class UserWebSocketController { // Add the token to the list this.pending_list.push({ time: time(), + clientID: h.getClientInfo().id, userID: h.getUserId(), token: token }); @@ -89,7 +102,23 @@ export class UserWebSocketController { const entry = this.pending_list[entryIndex]; this.pending_list.splice(entryIndex, 1); - console.log(entry) + // Add the client to the list of active clients + const client: ActiveClient = { + clientID: entry.clientID, + userID: entry.userID, + ws: ws + } + this.active_clients.push(client); + + + // Remove the client for the list as soon as the + // socket is closed + ws.addEventListener("close", () => { + this.active_clients.splice(this.active_clients.indexOf(client), 1); + }) + + + console.log(this.active_clients) } }