-
Notifications
You must be signed in to change notification settings - Fork 11
/
websocket-server.js
46 lines (35 loc) · 1.21 KB
/
websocket-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
42
43
44
45
46
const WebSocket = require('ws');
// Create a WebSocket server
const server = new WebSocket.Server({ port: 8088 });
// Maintain a list of connected sockets
const sockets = new Set();
// Handle WebSocket connection
server.on('connection', socket => {
console.log('WebSocket connection opened');
// Add the socket to the list of connected sockets
sockets.add(socket);
// Send initial message to the client
// socket.send('Hello from WebSocket server!');
// Listen for WebSocket messages
socket.on('message', message => {
console.log('WebSocket message received:', message);
// Echo the message back to the client
// socket.send(`You said: ${message}`);
// Push the message to all connected sockets except the sender
sockets.forEach(clientSocket => {
if (clientSocket !== socket) {
clientSocket.send(`${message}`);
}
});
});
// Listen for WebSocket close event
socket.on('close', () => {
console.log('WebSocket connection closed');
// Remove the socket from the list of connected sockets
sockets.delete(socket);
});
// Listen for WebSocket error event
socket.on('error', error => {
console.error('WebSocket error occurred:', error);
});
});