small refactor + web destination client

main
km0 1 year ago
parent 042fd52ad7
commit 38d45a7da8

@ -4,7 +4,7 @@
A small app for collecting drawings in real time. Runs on a small express server that connects sources (where to draw) and destinations (where to display) via websockets.
_(hei! this is still work in progress! currently adapting some aspects in the server in order to: a. make it more general, and not strictly related to vvvv, and b. make it possible to have more destinations connected to receive the drawings at the same time)_
_(hei! this is still work in progress! currently adapting some aspects in the server in order to: a. make it open-end and not strictly related to vvvv, and b. make it possible to have more destinations connected to receive the drawings at the same time)_
## Setup

@ -0,0 +1,61 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Draw draw draw</title>
<script src="wss.js"></script>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<h1>Display</h1>
<div id="svg-container" class="destination">
<svg
class="hidden"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
version="1.1"
id="svgElement"
x="0px"
y="0px"
width="200px"
height="200px"
viewBox="0 0 500 500"
enable-background="new 0 0 100 100"
preserveAspectRatio="none"
xml:space="preserve"
></svg>
</div>
<script>
const svgModel = document.querySelector("#svgElement");
const container = document.querySelector("#svg-container");
const socket = new ReconnectingWebSocket(location.origin.replace(/^http/, "ws"));
socket.onopen = (event) => {
socket.send(JSON.stringify({ type: "hello" }));
console.log("Connected as destination!");
};
socket.onmessage = (event) => {
let message;
try {
message = JSON.parse(event.data);
} catch (e) {}
if (message?.type == "drawings") {
let svg = svgModel.cloneNode();
svg.classList.remove("hidden");
let path = document.createElementNS("http://www.w3.org/2000/svg", "path");
path.setAttribute("d", message.paths);
path.setAttribute("fill", "none");
path.setAttribute("stroke", "currentColor");
path.setAttribute("stroke-width", 8);
svg.appendChild(path);
container.appendChild(svg);
}
};
</script>
</body>
</html>

@ -10,7 +10,7 @@
<link rel="stylesheet" href="style.css" />
</head>
<body>
<h1>Disegna <span id="theme"></span></h1>
<h1>Draw <span id="theme"></span></h1>
<div id="svg-container">
<svg
xmlns="http://www.w3.org/2000/svg"
@ -25,8 +25,8 @@
enable-background="new 0 0 500 500"
xml:space="preserve"
></svg>
<button id="submit">Spiedisci</button>
<button id="erase">Cancella</button>
<button id="submit">Send</button>
<button id="erase">Cancel</button>
</div>
<script>

@ -8,6 +8,10 @@ body {
background-color: white;
}
.destination #svgElement {
background: none;
}
button {
display: block;
background-color: white;
@ -18,6 +22,10 @@ button {
margin-top: 16px;
}
.hidden {
display: none;
}
#theme {
font-weight: bold;
-webkit-text-stroke: 1px black;

@ -6,45 +6,52 @@ const index = "index.html";
const server = express()
.use(express.static("public"))
.use((req, res) => res.sendFile(index, { root: __dirname }))
.get("/", (req, res) => {
res.sendFile("index.html", { root: __dirname });
})
.get("/destination", (req, res) => {
res.sendFile("destination.html", { root: __dirname });
})
.listen(PORT, () => console.log(`Listening on ${PORT}`));
const wss = new WebSocketServer({ server, clientTracking: true });
let VVVV = null;
let DESTINATIONS = new Set();
let USERS = new Set();
var theme = "";
const messageProcessor = {
default: (ws, msg) => unknownMsg(msg),
hello_v4: (ws, msg) => registerV4(ws, msg),
drawings: (ws, msg) => toV4(msg),
hello: (ws, msg) => registerDest(ws, msg),
drawings: (ws, msg) => toDest(msg),
theme: (ws, msg) => ((theme = msg.theme), broadcast(msg)),
};
const unknownMsg = (msg) => {
console.log("Unknown message type...");
console.log("Unknown message type...");
console.log(msg);
};
const registerV4 = (ws, msg) => {
if (VVVV == null) console.log("⬛ vvvv client connected");
VVVV = ws;
VVVV.on("close", () => {
VVVV = null;
const registerDest = (ws, msg) => {
console.log("Destination client connected");
DESTINATIONS.add(ws);
ws.on("close", () => {
DESTINATIONS.delete(ws);
});
};
const toV4 = (msg) => {
if (VVVV?.readyState === WebSocket.OPEN) {
VVVV.send(JSON.stringify(msg));
}
const toDest = (msg) => {
DESTINATIONS.forEach((DESTINATION) => {
if (DESTINATION?.readyState === WebSocket.OPEN) {
DESTINATION.send(JSON.stringify(msg));
}
});
};
const broadcast = (msg) => {
let message = JSON.stringify(msg);
for (const user of USERS.values()) {
if (user?.readyState === WebSocket.OPEN && user != VVVV) {
if (user?.readyState === WebSocket.OPEN && !DESTINATIONS.has(user)) {
user.send(message);
}
}

Loading…
Cancel
Save