Update server.js
Browse files
server.js
CHANGED
@@ -1,22 +1,24 @@
|
|
1 |
-
const
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
.listen(7860, () => console.log(`Listening on ${7860}`))
|
7 |
-
const { WebSocketServer } = require('ws')
|
8 |
-
const sockserver = new WebSocketServer({ port: 443 })
|
9 |
sockserver.on('connection', ws => {
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const { WebSocketServer } = require('ws');
|
2 |
+
|
3 |
+
// Create a WebSocket server listening on port 443
|
4 |
+
const sockserver = new WebSocketServer({ port: 443 });
|
5 |
+
|
|
|
|
|
|
|
6 |
sockserver.on('connection', ws => {
|
7 |
+
console.log('New client connected!');
|
8 |
+
ws.send('connection established');
|
9 |
+
|
10 |
+
ws.on('close', () => console.log('Client has disconnected!'));
|
11 |
+
|
12 |
+
ws.on('message', data => {
|
13 |
+
sockserver.clients.forEach(client => {
|
14 |
+
console.log(`distributing message: ${data}`);
|
15 |
+
client.send(`${data}`);
|
16 |
+
});
|
17 |
+
});
|
18 |
+
|
19 |
+
ws.onerror = function () {
|
20 |
+
console.log('websocket error');
|
21 |
+
};
|
22 |
+
});
|
23 |
+
|
24 |
+
console.log(`WebSocket server listening on port 443`);
|