Kano001 commited on
Commit
971fc9c
1 Parent(s): 5750188

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +23 -21
server.js CHANGED
@@ -1,22 +1,24 @@
1
- const express = require('express')
2
- const webserver = express()
3
- .use((req, res) =>
4
- res.sendFile('/websocket-client.html', { root: __dirname })
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
- console.log('New client connected!')
11
- ws.send('connection established')
12
- ws.on('close', () => console.log('Client has disconnected!'))
13
- ws.on('message', data => {
14
- sockserver.clients.forEach(client => {
15
- console.log(`distributing message: ${data}`)
16
- client.send(`${data}`)
17
- })
18
- })
19
- ws.onerror = function () {
20
- console.log('websocket error')
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`);