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(); this.clients = new Map();
} }
// Sets the server websocket instance
setServer(server_instance: WebSocket): void { setServer(server_instance: WebSocket): void {
this.server_instance = server_instance; this.server_instance = server_instance;
} }
// Gets the server websocket instance
getServer(): WebSocket | null { getServer(): WebSocket | null {
return this.server_instance; return this.server_instance;
} }
// Adds a client websocket instance to the map
addClient(client: WebSocket): string { addClient(client: WebSocket): string {
const id = v4(); const id = v4();
this.clients.set(id, { instance: client, timeout: null }); this.clients.set(id, { instance: client, timeout: null });
@@ -31,16 +34,7 @@ export class P2P {
return id; return id;
} }
getClient(id: string, client: WebSocket): WebSocket | null { // Returns client peer instance
const client_instance = this.clients.get(id)?.instance;
if (client_instance == client) {
return client_instance;
}
return null;
}
getPeer(id: string, client: WebSocket): WebSocket | null { getPeer(id: string, client: WebSocket): WebSocket | null {
if (client == this.server_instance) { if (client == this.server_instance) {
const client_instance = this.clients.get(id)?.instance; const client_instance = this.clients.get(id)?.instance;
@@ -51,10 +45,12 @@ export class P2P {
} }
} }
// Removes client entry from map
removeClient(id: string) { removeClient(id: string) {
return this.clients.delete(id); return this.clients.delete(id);
} }
// Returns client id
getId(client: WebSocket): string | null { getId(client: WebSocket): string | null {
var key = null; var key = null;
@@ -68,6 +64,7 @@ export class P2P {
return key; return key;
} }
// Clears the P2P instance
clear() { clear() {
this.clients.forEach((v) => { this.clients.forEach((v) => {
v.instance.close(3001, "Server shutdown."); v.instance.close(3001, "Server shutdown.");
@@ -76,6 +73,7 @@ export class P2P {
this.clients.clear(); 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) { setClientTimeout(id: string, p2p: P2P, ws_client: WebSocket, callback: (id: string, p2p: P2P, ws_client: WebSocket) => void) {
const instance = this.clients.get(id); const instance = this.clients.get(id);
if (instance) { if (instance) {
@@ -83,6 +81,7 @@ export class P2P {
} }
} }
// Clears client timeout
clearClientTimeout(id: string) { clearClientTimeout(id: string) {
const timeout = this.clients.get(id)?.timeout; const timeout = this.clients.get(id)?.timeout;
if (timeout && (timeout != null)) { if (timeout && (timeout != null)) {
@@ -90,6 +89,7 @@ export class P2P {
} }
} }
// Returns the total entries in the map
getMapSize(): number { getMapSize(): number {
return this.clients.size; return this.clients.size;
} }