1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-12-26 13:38:52 +00:00

Maintain a list of active connections

This commit is contained in:
Pierre HUBERT 2020-03-30 13:41:20 +02:00
parent fa5335f156
commit 4bfcb9b35b

View File

@ -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)
}
}