-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
42 lines (33 loc) · 1.02 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
33
34
35
36
37
38
39
40
41
import { WebSocketServer } from 'ws';
import http from 'http'
import { uuid } from 'uuidv4';
// Spinning the HTTP server and the WebSocket server.
const server = http.createServer();
const wsServer = new WebSocketServer({ server });
const port = 8000;
server.listen(port, () => {
console.log(`WebSocket server is running on port ${port}`);
});
// I'm maintaining all active connections in this object
const clients = [];
// A new client connection request received
wsServer.on('connection', function(ws) {
// Generate a unique code for every user
const userId = uuid();
console.log(`Recieved a new connection.`)
console.log(ws)
// Store the new connection and handle messages
clients[userId] = ws;
// console.log(`${userId} connected.`);
ws.on('message', function message(data) {
console.log('received: %s', data);
for (let key in clients){
clients[key].send(`${data}`)
console.log('sent')
}
console.log(clients.length)
});
ws.on('close', function close() {
ws.close()
});
});