forked from KlairSecure/websocket-server-poc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmytest.js
57 lines (45 loc) · 1.36 KB
/
mytest.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
47
48
49
50
51
52
53
54
55
56
57
const WebSocket = require('ws');
const async = require('async');
const numClients = 500;
const duration = 5 * 60 * 1000; // 5 minutes in milliseconds
const clients = [];
function createWebSocketClient(clientIndex) {
const ws = new WebSocket('ws://localhost:8080/ws');
ws.on('open', () => {
console.log(`Client ${clientIndex} connected`);
setInterval(() => {
// Send a message to the server every second
ws.send(`${clientIndex}: Hello from client`);
}, 1);
});
ws.on('message', (message) => {
console.log(`${clientIndex} ${message}`)
if (message === 'pong') {
// You can handle the received "ok" messages here if needed
}
});
ws.on('close', () => {
console.log(`Client ${clientIndex} disconnected`);
});
return ws;
}
function closeWebSocketClient(clientIndex, ws) {
ws.close();
console.log(`Client ${clientIndex} disconnected`);
}
function startWebSocketClients() {
for (let i = 0; i < numClients; i++) {
const wsClient = createWebSocketClient(i);
clients.push(wsClient);
}
}
function stopWebSocketClients() {
clients.forEach((ws, index) => {
closeWebSocketClient(index, ws);
});
}
startWebSocketClients();
setTimeout(() => {
stopWebSocketClients();
console.log('Test completed.');
}, duration);