Added comments

This commit is contained in:
2025-07-26 12:32:47 +03:00
parent 9fe48bf78d
commit 59ca501fd5
+10 -10
View File
@@ -16,14 +16,17 @@ export class P2P {
this.clients = new Map();
}
// Sets the server websocket instance
setServer(server_instance: WebSocket): void {
this.server_instance = server_instance;
}
// Gets the server websocket instance
getServer(): WebSocket | null {
return this.server_instance;
}
// Adds a client websocket instance to the map
addClient(client: WebSocket): string {
const id = v4();
this.clients.set(id, { instance: client, timeout: null });
@@ -31,16 +34,7 @@ export class P2P {
return id;
}
getClient(id: string, client: WebSocket): WebSocket | null {
const client_instance = this.clients.get(id)?.instance;
if (client_instance == client) {
return client_instance;
}
return null;
}
// Returns client peer instance
getPeer(id: string, client: WebSocket): WebSocket | null {
if (client == this.server_instance) {
const client_instance = this.clients.get(id)?.instance;
@@ -51,10 +45,12 @@ export class P2P {
}
}
// Removes client entry from map
removeClient(id: string) {
return this.clients.delete(id);
}
// Returns client id
getId(client: WebSocket): string | null {
var key = null;
@@ -68,6 +64,7 @@ export class P2P {
return key;
}
// Clears the P2P instance
clear() {
this.clients.forEach((v) => {
v.instance.close(3001, "Server shutdown.");
@@ -76,6 +73,7 @@ export class P2P {
this.clients.clear();
}
// Sets client timeout after register
setClientTimeout(id: string, p2p: P2P, ws_client: WebSocket, callback: (id: string, p2p: P2P, ws_client: WebSocket) => void) {
const instance = this.clients.get(id);
if (instance) {
@@ -83,6 +81,7 @@ export class P2P {
}
}
// Clears client timeout
clearClientTimeout(id: string) {
const timeout = this.clients.get(id)?.timeout;
if (timeout && (timeout != null)) {
@@ -90,6 +89,7 @@ export class P2P {
}
}
// Returns the total entries in the map
getMapSize(): number {
return this.clients.size;
}