mirror of
https://github.com/0015/ThatProject.git
synced 2026-01-12 17:27:43 +03:00
ESP32 CAM with Arduino IDE | Tutorial - [Part.8] Multiple CAM Dashboard (ft. Modified Jpeg Header)
This commit is contained in:
38
ESP32_CAM_MULTICAM/NodeServer/server.js
Normal file
38
ESP32_CAM_MULTICAM/NodeServer/server.js
Normal file
@@ -0,0 +1,38 @@
|
||||
const path = require("path");
|
||||
const express = require("express");
|
||||
const WebSocket = require("ws");
|
||||
const app = express();
|
||||
|
||||
const WS_PORT = 8888;
|
||||
const HTTP_PORT = 8000;
|
||||
|
||||
const wsServer = new WebSocket.Server({ port: WS_PORT }, () => console.log(`WS Server is listening at ${WS_PORT}`));
|
||||
|
||||
let connectedClients = [];
|
||||
wsServer.on("connection", (ws, req) => {
|
||||
console.log("Connected");
|
||||
|
||||
ws.on("message", (data) => {
|
||||
if (data.indexOf("WEB_CLIENT") !== -1) {
|
||||
connectedClients.push(ws);
|
||||
console.log("WEB_CLIENT ADDED");
|
||||
return;
|
||||
}
|
||||
|
||||
connectedClients.forEach((ws, i) => {
|
||||
if (connectedClients[i] == ws && ws.readyState === ws.OPEN) {
|
||||
ws.send(data);
|
||||
} else {
|
||||
connectedClients.splice(i, 1);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
ws.on("error", (error) => {
|
||||
console.error("WebSocket error observed: ", error);
|
||||
});
|
||||
});
|
||||
|
||||
app.use(express.static("."));
|
||||
app.get("/client", (req, res) => res.sendFile(path.resolve(__dirname, "./client.html")));
|
||||
app.listen(HTTP_PORT, () => console.log(`HTTP server listening at ${HTTP_PORT}`));
|
||||
Reference in New Issue
Block a user