-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
32 lines (26 loc) · 1.27 KB
/
server.js
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
31
32
const express = require('express');
const WebSocket = require('ws');
const app = express();
const port = 3000;
// Serve static files (main page and secondary device pages)
app.use(express.static('public'));
// WebSocket server
const wss = new WebSocket.Server({ server: app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
}) });
wss.on('connection', (ws) => {
console.log('A client connected');
wss.on('connection', (ws) => {
ws.on('message', (message) => {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
// Currently broadcasting message multiple times to the same client multiple times based on total connected clients
// This means that if you reload the main page multiple times and then send a single message from the console, the message will be recived mulitple times.
// AFAIK inital client count is 1 due to the server also counting as a client. (Client != connections but I'm saying they are.)
console.log('Broadcasting message:', message.toString());
client.send(message.toString()); // Ensure it's a string
}
});
});
});
});