-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathezy-sockets.js
37 lines (32 loc) · 1.08 KB
/
ezy-sockets.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
import Const from './ezy-constants';
class EzyPingSchedule {
constructor(client) {
this.client = client;
this.pingManager = client.pingManager;
this.eventMessageHandler = null;
}
start() {
var startPingNow = function (thiz) {
var pingInterval = setInterval(() => {
thiz.sendPingRequest();
}, thiz.pingManager.pingPeriod);
return pingInterval;
};
this.stop();
this.pingInterval = startPingNow(this);
}
sendPingRequest() {
const maxLostPingCount = this.pingManager.maxLostPingCount;
const lostPingCount = this.pingManager.increaseLostPingCount();
if (lostPingCount >= maxLostPingCount) {
var reason = Const.EzyDisconnectReason.SERVER_NOT_RESPONDING;
this.eventMessageHandler.handleDisconnection(reason);
} else {
this.client.sendRequest(Const.EzyCommand.PING, []);
}
}
stop() {
if (this.pingInterval) clearInterval(this.pingInterval);
}
}
export default { EzyPingSchedule };