Added comments

This commit is contained in:
2025-07-26 12:33:08 +03:00
parent 888ef441ae
commit 6354b4639f
+23 -1
View File
@@ -29,6 +29,11 @@ type ParseFailure = {
error: "Invalid JSON." | "Invalid Schema."; error: "Invalid JSON." | "Invalid Schema.";
}; };
/*
* Gets a JSON string, parses it and returns a JSON object.
* On success, the success key is true and the data key contains the parsed
* data. On failure, success is false and error key contains the error.
*/
function safeParse(raw: string): ParseSuccess | ParseFailure { function safeParse(raw: string): ParseSuccess | ParseFailure {
let parsed; let parsed;
@@ -47,17 +52,22 @@ function safeParse(raw: string): ParseSuccess | ParseFailure {
} }
} }
// Handler function that kicks inactive clients.
function timeoutHandler(id: string, p2p: P2P, ws_client: WebSocket) { function timeoutHandler(id: string, p2p: P2P, ws_client: WebSocket) {
ws_client.close(WS_CODE.TIMEOUT, "Timeout."); ws_client.close(WS_CODE.TIMEOUT, "Timeout.");
logger.info("Kicked client due to inactivity."); logger.info("Kicked client due to inactivity.");
p2p.removeClient(id); p2p.removeClient(id);
} }
// Handles register event
function register(p2p: P2P, ws_client: WebSocket, data: Register) { function register(p2p: P2P, ws_client: WebSocket, data: Register) {
const { username, password } = data; const { username, password } = data;
// Check if client credentials where provided
if ((username == config.authentication.client.username) && (password == config.authentication.client.password)) { if ((username == config.authentication.client.username) && (password == config.authentication.client.password)) {
// Check if the server is online
if (p2p.getServer() != null) { if (p2p.getServer() != null) {
// Add client to the map and start the timer
const id = p2p.addClient(ws_client); const id = p2p.addClient(ws_client);
p2p.setClientTimeout(id, p2p, ws_client, timeoutHandler); p2p.setClientTimeout(id, p2p, ws_client, timeoutHandler);
@@ -68,6 +78,7 @@ function register(p2p: P2P, ws_client: WebSocket, data: Register) {
logger.error("Client connected but server offline."); logger.error("Client connected but server offline.");
ws_client.close(WS_CODE.BACKEND_OFFLINE, "Backend offline."); ws_client.close(WS_CODE.BACKEND_OFFLINE, "Backend offline.");
} }
// Check if server credentials where provided and set the server
} else if ((username == config.authentication.server.username) && (password == config.authentication.server.password)) { } else if ((username == config.authentication.server.username) && (password == config.authentication.server.password)) {
logger.info("Server set."); logger.info("Server set.");
p2p.setServer(ws_client); p2p.setServer(ws_client);
@@ -77,10 +88,12 @@ function register(p2p: P2P, ws_client: WebSocket, data: Register) {
} }
} }
// Handles offer event
function offer(p2p: P2P, ws_client: WebSocket, data: Offer): void { function offer(p2p: P2P, ws_client: WebSocket, data: Offer): void {
const { id, offer } = data; const { id, offer } = data;
const peer = p2p.getPeer(id, ws_client); const peer = p2p.getPeer(id, ws_client);
// Get the peer and forward the offer to it
if (peer != null) { if (peer != null) {
p2p.clearClientTimeout(id); p2p.clearClientTimeout(id);
logger.info(`Forwarded offer, reset timeout ID: [${id}].`); logger.info(`Forwarded offer, reset timeout ID: [${id}].`);
@@ -90,10 +103,12 @@ function offer(p2p: P2P, ws_client: WebSocket, data: Offer): void {
} }
} }
// Handles answer event
function answer(p2p: P2P, ws_client: WebSocket, data: Answer): void { function answer(p2p: P2P, ws_client: WebSocket, data: Answer): void {
const { id, answer } = data; const { id, answer } = data;
const peer = p2p.getPeer(id, ws_client); const peer = p2p.getPeer(id, ws_client);
// Get the peer and forward the answer to it
if (peer != null) { if (peer != null) {
logger.info(`Forwarded answer ID: [${id}].`); logger.info(`Forwarded answer ID: [${id}].`);
peer.send(JSON.stringify({ type: "answer", id: id, answer: answer })); peer.send(JSON.stringify({ type: "answer", id: id, answer: answer }));
@@ -102,10 +117,12 @@ function answer(p2p: P2P, ws_client: WebSocket, data: Answer): void {
} }
} }
// Handles candidate event
function candidate(p2p: P2P, ws_client: WebSocket, data: Candidate): void { function candidate(p2p: P2P, ws_client: WebSocket, data: Candidate): void {
const { id, candidate } = data; const { id, candidate } = data;
const peer = p2p.getPeer(id, ws_client); const peer = p2p.getPeer(id, ws_client);
// Get the peer and forward the candidate to it
if (peer != null) { if (peer != null) {
logger.info(`Forwarded candidate ID: [${id}].`); logger.info(`Forwarded candidate ID: [${id}].`);
peer.send(JSON.stringify({ type: "candidate", id: id, candidate: candidate })); peer.send(JSON.stringify({ type: "candidate", id: id, candidate: candidate }));
@@ -115,13 +132,17 @@ function candidate(p2p: P2P, ws_client: WebSocket, data: Candidate): void {
} }
(() => { (() => {
const wss = new WebSocket.WebSocketServer({ port: 3000 }); // Start the websocket server
const wss = new WebSocket.WebSocketServer({ port: config.wss_port });
logger.info(`Started WebSocket server. Listening on port: ${config.wss_port}`)
// Create the P2P instance
const p2p = new P2P(); const p2p = new P2P();
logger.info("Created P2P instance."); logger.info("Created P2P instance.");
wss.on("connection", async (ws_client) => { wss.on("connection", async (ws_client) => {
ws_client.on("message", async (ws_msg) => { ws_client.on("message", async (ws_msg) => {
// Parse received message and call the corresponding handler
const parsed = safeParse(ws_msg.toString()); const parsed = safeParse(ws_msg.toString());
if (parsed.success) { if (parsed.success) {
@@ -141,6 +162,7 @@ function candidate(p2p: P2P, ws_client: WebSocket, data: Candidate): void {
} }
}); });
// Remove the client on disconnect and inform the peer
ws_client.on("close", (code, reason) => { ws_client.on("close", (code, reason) => {
if ((code != WS_CODE.INVALID_CREDENTIALS) && if ((code != WS_CODE.INVALID_CREDENTIALS) &&
(code != WS_CODE.BACKEND_OFFLINE) && (code != WS_CODE.BACKEND_OFFLINE) &&