-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
30 lines (24 loc) · 825 Bytes
/
Copy pathserver.js
File metadata and controls
30 lines (24 loc) · 825 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
import { WebSocketServer } from 'ws'; // Use WebSocketServer for ES module imports
const wss = new WebSocketServer({
host:"0.0.0.0",
port: 8080 }); // Create WebSocket server
let clients = [];
wss.on('connection', (ws) => {
// Add connected client to clients array
clients.push(ws);
console.log("New client connected");
// Listen for messages from client (streamer/viewer)
ws.on('message', (message) => {
// Broadcast incoming message to all clients except the sender
for (const client of clients) {
if (client !== ws) {
client.send(message);
}
}
});
// When client disconnects, remove them from the clients array
ws.on('close', () => {
clients = clients.filter(client => client !== ws);
});
});
console.log('WebSocket server running on ws://localhost:8080');