-
Notifications
You must be signed in to change notification settings - Fork 0
/
client-subscribers.js
65 lines (53 loc) · 1.58 KB
/
client-subscribers.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
let socketClusterClient = require('socketcluster-client');
let argv = require('minimist')(process.argv.slice(2));
let options = JSON.parse(argv.options || '{}');
let clientOptions = {
port: options.targetPort || 8000,
hostname: options.targetHost || '127.0.0.1'
};
let clientCount = options.clientCount || 1;
let uniqueChannelCount = options.uniqueChannelCount || 100;
let channelsPerClient = options.channelsPerClient || 1;
let channelNames = [];
for (let i = 0; i < uniqueChannelCount; i++) {
channelNames.push(`testChannel${i}`);
}
let pendingSubscriptionPromises = [];
let c = 0;
for (let i = 0; i < clientCount; i++) {
let socket = socketClusterClient.create(clientOptions);
(async () => {
for await (let {error} of socket.listener('error')) {
process.send({
type: 'error',
name: error.name,
message: error.message
});
}
})();
for (let j = 0; j < channelsPerClient; j++) {
let targetChannelName = channelNames[c++ % uniqueChannelCount];
if (socket.isSubscribed(targetChannelName, true)) {
continue;
}
let channel = socket.subscribe(targetChannelName);
pendingSubscriptionPromises.push(channel.listener('subscribe').once());
(async () => {
for await (let data of channel) {
process.send({
type: 'received',
socketId: socket.id,
channel: channel.name,
data
});
}
})();
}
}
async function waitForSubscribersToBeReady() {
await Promise.all(pendingSubscriptionPromises);
process.send({
type: 'ready'
});
}
waitForSubscribersToBeReady();