mirror of
https://github.com/0015/ThatProject.git
synced 2026-01-13 01:37:43 +03:00
Broadcasting Your Voice with ESP32-S3 & INMP441
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
/////////////////////////////////////////////////////////////////
|
||||
/*
|
||||
Broadcasting Your Voice with ESP32-S3 & INMP441
|
||||
For More Information: https://youtu.be/qq2FRv0lCPw
|
||||
Created by Eric N. (ThatProject)
|
||||
*/
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
const path = require("path");
|
||||
const express = require("express");
|
||||
const WebSocket = require("ws");
|
||||
const app = express();
|
||||
|
||||
const WS_PORT = process.env.WS_PORT || 8888;
|
||||
const HTTP_PORT = process.env.HTTP_PORT || 8000;
|
||||
|
||||
const wsServer = new WebSocket.Server({ port: WS_PORT }, () =>
|
||||
console.log(`WS server is listening at ws://localhost:${WS_PORT}`)
|
||||
);
|
||||
|
||||
// array of connected websocket clients
|
||||
let connectedClients = [];
|
||||
|
||||
wsServer.on("connection", (ws, req) => {
|
||||
console.log("Connected");
|
||||
// add new connected client
|
||||
connectedClients.push(ws);
|
||||
// listen for messages from the streamer, the clients will not send anything so we don't need to filter
|
||||
ws.on("message", (data) => {
|
||||
connectedClients.forEach((ws, i) => {
|
||||
if (ws.readyState === ws.OPEN) {
|
||||
ws.send(data);
|
||||
} else {
|
||||
connectedClients.splice(i, 1);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// HTTP stuff
|
||||
app.use("/image", express.static("image"));
|
||||
app.use("/js", express.static("js"));
|
||||
app.get("/audio", (req, res) =>
|
||||
res.sendFile(path.resolve(__dirname, "./audio_client.html"))
|
||||
);
|
||||
app.listen(HTTP_PORT, () =>
|
||||
console.log(`HTTP server listening at http://localhost:${HTTP_PORT}`)
|
||||
);
|
||||
Reference in New Issue
Block a user