forked from ClusterWS/cWS
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathecho.js
36 lines (29 loc) · 859 Bytes
/
echo.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
const { WebSocket: { Server } } = require('../dist');
const WebSocket = require('ws');
const wss = new Server({ port: 3000 }, () => {
console.log('Server is running on port 3000');
const ws = new WebSocket('ws://localhost:3000');
ws.on('open', function open() {
console.log('Connected to the server');
ws.send('Hello, server!');
});
ws.on('message', function incoming(data) {
console.log(`Received echoed message: ${data}`);
// Close the connection after receiving the echoed message
ws.close();
});
ws.on('close', () => {
console.log('Disconnected from the server');
});
}, 0);
wss.on('connection', (ws, req) => {
ws.on('message', (msg) => {
ws.send(msg);
});
ws.on('close', () => {
setTimeout(() => {
console.log('Client disconncted, quitting');
wss.close();
}, 10);
});
});