-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.js
65 lines (51 loc) · 1.63 KB
/
client.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
58
59
60
61
62
63
64
65
var desiredConnections = 50000;
var serverUrl = 'ws://142.93.131.241/chatserver';
var WebSocketClient = require('websocket').client;
var connectionsCount = 0;
function sleep (time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
function connect() {
var client = new WebSocketClient();
client.on('connectFailed', function(error) {
console.log('Connect Error: ' + error.toString());
});
client.on('connect', function(connection) {
console.log('Client Connected. total = '+ connectionsCount);
connectionsCount++;
connection.on('error', function(error) {
connectionsCount--;
connection.disconnect();
console.log("Connection Error: " + error.toString()+' total = '+ connectionsCount);
});
connection.on('close', function() {
connectionsCount--;
console.log("Connection Closed. total = "+ connectionsCount);
});
connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log("Received: '" + message.utf8Data + "'");
}
});
// function sendNumber() {
// if (connection.connected) {
// var number = Math.round(Math.random() * 0xFFFFFF);
// connection.sendUTF(number.toString());
// setTimeout(sendNumber, 1000);
// }
// }
// sendNumber();
if (desiredConnections > connectionsCount) {
/*if (connectionsCount % 100 == 0) {
sleep(200).then(() => {
connect();
});
} else {
connect();
}*/
connect();
}
});
client.connect(serverUrl);
}
connect();