/
ping.ts
105 lines (78 loc) · 3.49 KB
/
ping.ts
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
This file is part of botten-nappet -- a Twitch bot and streaming tool.
<https://joelpurra.com/projects/botten-nappet/>
Copyright (c) 2018 Joel Purra <https://joelpurra.com/>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {
assert,
} from "check-types";
import PinoLogger from "@botten-nappet/shared/util/pino-logger";
import IPubSubConnection from "../connection/ipubsub-connection";
import PubSubManager from "../connection/pubsub-manager";
import IPubSubResponse from "../interface/ipubsub-response";
export default class PingPubSubHandler extends PubSubManager {
public pubSubConnection: IPubSubConnection;
private pingIntervalMilliseconds: number;
private pingIntervalId: (number | NodeJS.Timer | null);
constructor(logger: PinoLogger, connection: IPubSubConnection) {
super(logger, connection);
assert.hasLength(arguments, 2);
assert.equal(typeof logger, "object");
assert.equal(typeof connection, "object");
this.logger = logger.child("PingPubSubHandler");
this.pingIntervalId = null;
this.pubSubConnection = this.connection as IPubSubConnection;
// NOTE: minimum interval is a PING every 5 minutes.
// https://dev.twitch.tv/docs/pubsub#connection-management
this.pingIntervalMilliseconds = 4 * 60 * 1000;
}
public async start(): Promise<void> {
assert.hasLength(arguments, 0);
assert.equal(this.pingIntervalId, null);
await super.start();
// TODO: configure atBegin?
await this.ping();
// TODO: use an observable interval?
this.pingIntervalId = setInterval(() => this.ping(), this.pingIntervalMilliseconds);
}
public async stop(): Promise<void> {
assert.hasLength(arguments, 0);
assert.not.equal(this.pingIntervalId, null);
clearInterval(this.pingIntervalId as NodeJS.Timer);
this.pingIntervalId = null;
return super.stop();
}
protected async dataHandler(data: IPubSubResponse): Promise<void> {
assert.hasLength(arguments, 1);
assert.equal(typeof data, "object");
this.logger.trace(data, "dataHandler");
throw new Error("Unexpected call to dataHandler.");
}
protected async filter(data: IPubSubResponse): Promise<boolean> {
assert.hasLength(arguments, 1);
assert.equal(typeof data, "object");
// TODO: check if the most recent ping was sent within 15 seconds, otherwise delay and reconnect.
// TODO: backoff doubling for reconnects.
// https://dev.twitch.tv/docs/pubsub#connection-management
return false;
}
private async ping(): Promise<void> {
assert.hasLength(arguments, 0);
this.logger.trace("Sending ping", "ping");
const message = {
type: "PING",
};
// TODO: handle errors, re-reconnect, or shut down server?
return this.pubSubConnection.send(message);
}
}