This repository has been archived by the owner on Apr 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
local_chat_server.js
179 lines (157 loc) · 5.32 KB
/
local_chat_server.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env node
var WebSocketServer = require('websocket').server;
var http = require('http');
var server = http.createServer(function(request, response) {
console.log((new Date()) + ' Received request for ' + request.url);
response.writeHead(404);
response.end();
});
server.listen(8080, function() {
console.log((new Date()) + ' Server is listening on port 8080');
});
wsServer = new WebSocketServer({
httpServer: server,
// You should not use autoAcceptConnections for production
// applications, as it defeats all standard cross-origin protection
// facilities built into the protocol and the browser. You should
// *always* verify the connection's origin and decide whether or not
// to accept it.
autoAcceptConnections: false
});
function originIsAllowed(origin) {
// put logic here to detect whether the specified origin is allowed.
return true;
}
function sendHeartMessage(connection, user) {
var msg = '{' +
'"id":"28888ee0-14fc-42cf-842e-559b70909b91",' +
'"type":"heart",' +
'"user":{' +
'"id":"' + user.userId + '"' +
'},' +
'"color":"#F5A623"}';
connection.sendUTF(msg);
//console.log("heart | userId: " + user.userId);
}
function sendJoinMessage(connection, user) {
var msg = '{' +
'"id":"c9485103-ef4a-4c2e-a529-8cc39b1f9275",' +
'"type":"join",' +
'"user":{' +
'"id":"' + user.userId + '",' +
'"username":"' + user.username + '",' +
'"display_name":"asdkjashd",' +
'"profile_image_urls":[{"url":"https://pbs.twimg.com/profile_images/565642159492063232/ltKt547A_reasonably_small.jpeg"}],' +
'"locale":"en",' +
'"languages":["en"],' +
'"superfan":true' +
'},' +
'"color":"#F5A623"}';
connection.sendUTF(msg);
//console.log("join | userId: " + user.userId);
}
var mmm = 0;
function sendChatMessage(connection, user) {
var msg = '{' +
'"id":"c9485103-ef4a-4c2e-a529-8cc39b1f9275",' +
'"type":"chat",' +
'"text":"' + user.message + '",' +
'"user":{' +
'"id":"' + user.userId + '",' +
'"username":"' + user.username + '",' +
'"display_name":"asdkjashd",' +
'"profile_image_urls":[{"url":"https://pbs.twimg.com/profile_images/565642159492063232/ltKt547A_reasonably_small.jpeg"}],' +
'"locale":"en",' +
'"languages":["en"],' +
'"superfan":true' +
'},' +
'"color":"#F5A623"}';
mmm++;
connection.sendUTF(msg);
//console.log("chat | userId: " + user.userId);
}
function shuffle(a) {
var j, x, i;
for (i = a.length; i; i--) {
j = randInt(i);
x = a[i - 1];
a[i - 1] = a[j];
a[j] = x;
}
}
function randInt(max) {
return Math.floor(Math.random() * max)
}
function randText(length) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < length; i++ )
text += possible.charAt(randInt(possible.length));
return text;
}
function sendMessages(connection, interval, numJoins, numHearts, numChats) {
var users = []
for (var i=0; i < numUsers; i++) {
users.push({
'participantIndex': randInt(9999999),
'userId': randText(13),
'username': randText(randInt(12) + 1),
'message': randText(randInt(25) + 10),
});
}
var funcIds = {};
var j = 0;
var numMessagesPerUser = numJoins + numHearts + numChats;
var numMessages = users.length * numMessagesPerUser;
console.log("sending " + numMessages + " messages from " + users.length + " users");
console.log("average qps: " + numMessages / (interval / 1000));
var execOrder = [];
for (var i=0; i<numMessages; i++) {
execOrder.push(Math.floor(i/numMessagesPerUser));
}
shuffle(execOrder);
for (var i=0; i<users.length; i++) {
for (var k=0; k<numJoins; k++) {
setTimeout(function () { sendJoinMessage(connection, users[execOrder[j]]); j++; }, randInt(interval));
}
for (var k=0; k<numHearts; k++) {
setTimeout(function () { sendHeartMessage(connection, users[execOrder[j]]); j++; }, randInt(interval));
}
for (var k=0; k<numChats; k++) {
setTimeout(function () { sendChatMessage(connection, users[execOrder[j]]); j++; }, randInt(interval));
}
}
}
// each user sends 1 join + 1 chat + numHearts hearts within interval
var numUsers = 40000;
var interval = 80000; // in milliseconds
var numJoins = 1;
var numHearts = 9;
var numChats = 1;
var intervals = {};
wsServer.on('request', function(request) {
if (!originIsAllowed(request.origin)) {
// Make sure we only accept requests from an allowed origin
request.reject();
console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
return;
}
var connection = request.accept(null, request.origin);
console.log((new Date()) + ' Connection accepted.');
connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log('Received Message: ' + message.utf8Data);
}
else if (message.type === 'binary') {
console.log('Received Binary Message of ' + message.binaryData.length + ' bytes');
}
});
connection.on('close', function(reasonCode, description) {
clearInterval(intervals[connection.remoteAddress]);
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
});
sendMessages(connection, interval, numJoins, numHearts, numChats);
intervals[connection.remoteAddress] = setInterval(function () {
sendMessages(connection, interval, numJoins, numHearts, numChats);
}, interval);
});