-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
30 lines (26 loc) · 764 Bytes
/
server.js
File metadata and controls
30 lines (26 loc) · 764 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const WebSocket = require("ws");
const express = require("express");
const path = require("path");
const app = express();
app.use("/", express.static(path.join(__dirname, "public")));
const myServer = app.listen(9876);
const wsServer = new WebSocket.Server({
noServer: true,
});
wsServer.on("connection", (ws) => {
ws.on("message", (msg) => {
let clientCount = 0;
wsServer.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
clientCount++;
client.send(msg.toString());
}
});
// console.log({ clientCount });
});
});
myServer.on("upgrade", async (request, socket, head) => {
wsServer.handleUpgrade(request, socket, head, (ws) => {
wsServer.emit("connection", ws, request);
});
});