You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.5 KiB
JavaScript

// Import dependencies
2 years ago
const { WebSocket, WebSocketServer } = require("ws");
const express = require("express");
2 years ago
const dotenv = require("dotenv");
dotenv.config();
// Setup Environmental Variables
2 years ago
const PORT = process.env.PORT || 3000;
const PREFIX = process.env.PREFIX || "";
const PUBLIC = process.env.PUBLIC || "";
2 years ago
// Setup Express Router
// Create the routes of the application
// Here are two pages and a wildcard:
// at / there is the index.html page, where to draw
// at /destination there is the destination.html page, where to receive the drawings
// the wildcard /* serves the static files from the public folder
const router = express.Router();
const routes = (app) => {
app.get("/", (req, res) => {
res.render("index", {
address: PREFIX,
});
});
app.get("/destination", (req, res) => {
res.render("destination", {
address: PREFIX,
});
});
2 years ago
app.get("/*", (req, res) => {
res.sendFile(req.url, { root: "public" });
});
return app;
};
// Setup the Express server
const server = express()
.set("view engine", "html")
.engine("html", require("hbs").__express)
.use(PREFIX, routes(router, {}))
.use(express.static("public"))
2 years ago
.listen(PORT, () => console.log(`Listening on ${PORT}`));
// Setup the Websocket server on top of the Express one
2 years ago
const wss = new WebSocketServer({ server, clientTracking: true });
// Global variables to manage the connected Destination and User clients
let DESTINATIONS = new Set();
2 years ago
let USERS = new Set();
var theme = "";
// The message processor defines which function is associated to every websocket message type.
// It map a type to a function, passing some optional parameters such as the message itself and the websocket client that sent it.
// for example an incoming message like {type: hello} will trigger the registerDest(ws, msg) function.
// In this way to add message types and functionalities gets easier, and avoid long chain of if-else statements.
2 years ago
const messageProcessor = {
default: (ws, msg) => unknownMsg(msg),
hello: (ws, msg) => registerDest(ws, msg),
drawings: (ws, msg) => toDest(msg),
2 years ago
theme: (ws, msg) => ((theme = msg.theme), broadcast(msg)),
};
// Message processor functions
// Default function, to cactch unkown message types
2 years ago
const unknownMsg = (msg) => {
console.log("Unknown message type...");
2 years ago
console.log(msg);
};
// Add the ws client in the destinations set
// Removing it when it disconnets
const registerDest = (ws, msg) => {
console.log("Destination client connected");
DESTINATIONS.add(ws);
ws.on("close", () => {
DESTINATIONS.delete(ws);
2 years ago
});
};
// Send a message to all the connected Destinations
const toDest = (msg) => {
let message = JSON.stringify(msg);
DESTINATIONS.forEach((DESTINATION) => {
if (DESTINATION?.readyState === WebSocket.OPEN) {
DESTINATION.send(message);
}
});
2 years ago
};
// Send a message to all the connected Users
2 years ago
const broadcast = (msg) => {
let message = JSON.stringify(msg);
for (const user of USERS.values()) {
if (user?.readyState === WebSocket.OPEN && !DESTINATIONS.has(user)) {
2 years ago
user.send(message);
}
}
};
// Websocket events listener
2 years ago
wss.on("connection", (ws) => {
USERS.add(ws);
ws.send(JSON.stringify({ type: "theme", theme: theme }));
ws.on("message", (data) => {
// Parse the incoming data safely
2 years ago
let message;
try {
message = JSON.parse(data);
} catch (e) {}
// Call the message processor, eventually falling back to use the default function if the type is not defined.
2 years ago
if (message) {
(messageProcessor[message?.type] || messageProcessor.default)(ws, message);
2 years ago
}
});
ws.on("close", () => {
USERS.delete(ws);
});
});