Added comments
This commit is contained in:
@@ -29,6 +29,11 @@ type ParseFailure = {
|
||||
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 {
|
||||
let parsed;
|
||||
|
||||
@@ -47,19 +52,24 @@ function safeParse(raw: string): ParseSuccess | ParseFailure {
|
||||
}
|
||||
}
|
||||
|
||||
// Handler function that kicks inactive clients.
|
||||
function timeoutHandler(id: string, p2p: P2P, ws_client: WebSocket) {
|
||||
ws_client.close(WS_CODE.TIMEOUT, "Timeout.");
|
||||
logger.info("Kicked client due to inactivity.");
|
||||
p2p.removeClient(id);
|
||||
}
|
||||
|
||||
// Handles register event
|
||||
function register(p2p: P2P, ws_client: WebSocket, data: Register) {
|
||||
const { username, password } = data;
|
||||
|
||||
// Check if client credentials where provided
|
||||
if ((username == config.authentication.client.username) && (password == config.authentication.client.password)) {
|
||||
// Check if the server is online
|
||||
if (p2p.getServer() != null) {
|
||||
// Add client to the map and start the timer
|
||||
const id = p2p.addClient(ws_client);
|
||||
|
||||
|
||||
p2p.setClientTimeout(id, p2p, ws_client, timeoutHandler);
|
||||
logger.info(`Client registered, timeout set and assigned ID: [${id}].`);
|
||||
logger.info(`Registered clients: ${p2p.getMapSize()}.`);
|
||||
@@ -68,6 +78,7 @@ function register(p2p: P2P, ws_client: WebSocket, data: Register) {
|
||||
logger.error("Client connected but server 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)) {
|
||||
logger.info("Server set.");
|
||||
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 {
|
||||
const { id, offer } = data;
|
||||
const peer = p2p.getPeer(id, ws_client);
|
||||
|
||||
// Get the peer and forward the offer to it
|
||||
if (peer != null) {
|
||||
p2p.clearClientTimeout(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 {
|
||||
const { id, answer } = data;
|
||||
const peer = p2p.getPeer(id, ws_client);
|
||||
|
||||
// Get the peer and forward the answer to it
|
||||
if (peer != null) {
|
||||
logger.info(`Forwarded answer ID: [${id}].`);
|
||||
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 {
|
||||
const { id, candidate } = data;
|
||||
const peer = p2p.getPeer(id, ws_client);
|
||||
|
||||
// Get the peer and forward the candidate to it
|
||||
if (peer != null) {
|
||||
logger.info(`Forwarded candidate ID: [${id}].`);
|
||||
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();
|
||||
logger.info("Created P2P instance.");
|
||||
|
||||
wss.on("connection", async (ws_client) => {
|
||||
ws_client.on("message", async (ws_msg) => {
|
||||
// Parse received message and call the corresponding handler
|
||||
const parsed = safeParse(ws_msg.toString());
|
||||
|
||||
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) => {
|
||||
if ((code != WS_CODE.INVALID_CREDENTIALS) &&
|
||||
(code != WS_CODE.BACKEND_OFFLINE) &&
|
||||
|
||||
Reference in New Issue
Block a user