-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathln-test.js
executable file
·63 lines (48 loc) · 1.42 KB
/
ln-test.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
var WebSocket = require('ws');
var t_ = require('./jTime.js');
var PING_TIME = 20000;
var DELAY_TIME = 1.5;
var MAX_RETRIES = 10;
var wsurl = 'wss://beans4bits.com';
var connection;
openSocket(0);
function isConnected() {
connection.send('ping!');
if (connection.readyState != 1) {
console.log('Disconnected. Reconnecting...');
openSocket(0);
}
}
t_.setInterval(600000, isConnected)
function openSocket(reconnectAttempts){
if (reconnectAttempts > MAX_RETRIES) {
console.log('Max retries reached');
return
}
connection = new WebSocket(wsurl);
var localConn = connection;
var timeout = setTimeout(function() {
console.log('ReconnectingWebSocket timed out');
localConn.close();
openSocket(reconnectAttempts++);
}, PING_TIME*10);
connection.on('open', function() {
clearTimeout(timeout);
console.log('Connected');
connection.send('Hello from client');
});
connection.on('message', function(data, flags) {
console.log(data);
});
connection.on('ping', function () {
});
connection.on('error', function (error) {
clearTimeout(timeout);
var longTime = PING_TIME * Math.pow(DELAY_TIME, reconnectAttempts);
setTimeout (openSocket(reconnectAttempts++), longTime);
});
connection.on('close', function () {
console.log('closed, reconnecting');
setTimeout (openSocket(0), PING_TIME);
});
}