Broadcasting Your Voice with ESP32-S3 & INMP441

This commit is contained in:
Eric
2023-05-16 15:15:24 -07:00
parent ea26810ea0
commit de2b756bd6
7 changed files with 1227 additions and 0 deletions

View File

@@ -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}`)
);