forked from Bitcoin-com/bitbox-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
countConnectionsSocketIO.js
55 lines (39 loc) · 1.48 KB
/
countConnectionsSocketIO.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
const BITBOX = require("../../../lib/BITBOX").BITBOX;
const { exec } = require('child_process');
const bitbox = new BITBOX();
const socket = new bitbox.Socket();
function countSockets(stage) {
return new Promise((resolve, reject) => {
// Call the lsof system command for outgoing internet connections.
exec(`lsof -i -n -P | grep ${process.pid}`, (err, stdout, stderr) => {
// Print list of open connections allowing a visual count to be done.
console.log(`Outbound connections from this node process ${stage}:\n${stdout}`);
resolve();
});
});
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
(async () => {
await countSockets("before calling listen");
//First call to listen() which should create new connection.
socket.listen("transactions", (message) => {
console.log("Received a transaction.");
});
//Second call to listen() which should share connection with first call.
socket.listen("blocks", (message) => {
console.log("Received a block.");
});
// listen doesn't return a promise so wait 100ms for connections to establish.
await sleep(100);
await countSockets("after calling listen twice");
// now close the socket
socket.close();
// callback from close() is short-circuited so give it 100ms to clean up.
await sleep(100);
// check if any zombie connections remaining
await countSockets("after calling close (zombie connections)");
// exit process
process.exit();
})();