1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const get_timer_1 = __importDefault(require("./get-timer"));
|
7 | class KeepaliveManager {
|
8 | get keepaliveTimeoutTimestamp() {
|
9 | return this._keepaliveTimeoutTimestamp;
|
10 | }
|
11 | get intervalEvery() {
|
12 | return this._intervalEvery;
|
13 | }
|
14 | get keepalive() {
|
15 | return this._keepalive;
|
16 | }
|
17 | constructor(client, variant) {
|
18 | this.destroyed = false;
|
19 | this.client = client;
|
20 | this.timer = (0, get_timer_1.default)(variant);
|
21 | this.setKeepalive(client.options.keepalive);
|
22 | }
|
23 | clear() {
|
24 | if (this.timerId) {
|
25 | this.timer.clear(this.timerId);
|
26 | this.timerId = null;
|
27 | }
|
28 | }
|
29 | setKeepalive(value) {
|
30 | value *= 1000;
|
31 | if (isNaN(value) ||
|
32 | value <= 0 ||
|
33 | value > 2147483647) {
|
34 | throw new Error(`Keepalive value must be an integer between 0 and 2147483647. Provided value is ${value}`);
|
35 | }
|
36 | this._keepalive = value;
|
37 | this.reschedule();
|
38 | this.client['log'](`KeepaliveManager: set keepalive to ${value}ms`);
|
39 | }
|
40 | destroy() {
|
41 | this.clear();
|
42 | this.destroyed = true;
|
43 | }
|
44 | reschedule() {
|
45 | if (this.destroyed) {
|
46 | return;
|
47 | }
|
48 | this.clear();
|
49 | this.counter = 0;
|
50 | const keepAliveTimeout = Math.ceil(this._keepalive * 1.5);
|
51 | this._keepaliveTimeoutTimestamp = Date.now() + keepAliveTimeout;
|
52 | this._intervalEvery = Math.ceil(this._keepalive / 2);
|
53 | this.timerId = this.timer.set(() => {
|
54 | if (this.destroyed) {
|
55 | return;
|
56 | }
|
57 | this.counter += 1;
|
58 | if (this.counter === 2) {
|
59 | this.client.sendPing();
|
60 | }
|
61 | else if (this.counter > 2) {
|
62 | this.client.onKeepaliveTimeout();
|
63 | }
|
64 | }, this._intervalEvery);
|
65 | }
|
66 | }
|
67 | exports.default = KeepaliveManager;
|
68 |
|
\ | No newline at end of file |