/
index.js
51 lines (40 loc) · 1.62 KB
/
index.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
const Promise = require("bluebird");
const TwitchPubSubConnection = require("./src/twitch/pubsub-connection");
const TwitchPubSubManager = require("./src/twitch/pubsub-manager");
// TODO: better token/config handling.
const twitchWebSocketUri = "wss://pubsub-edge.twitch.tv/";
const twitchAppAccessToken = process.env["TWITCH_APP_ACCESS_TOKEN"];
const twitchUserAccessToken = process.env["TWITCH_USER_ACCESS_TOKEN"];
const twitchChannelId = 148460096;
const MAX_LISTEN_TIME_MILLISECONDS = 5 * 1000;
const twitchPubSubConnection = new TwitchPubSubConnection(twitchWebSocketUri);
const twitchPubSubManager = new TwitchPubSubManager(twitchPubSubConnection, twitchChannelId, twitchUserAccessToken);
Promise.resolve().then(() => twitchPubSubConnection.connect()).then(() => {
const disconnect = (error) => twitchPubSubConnection.disconnect().then(() => {
if (error) {
console.error("Disconnected.", error);
} else {
console.log("Disconnected.");
}
return undefined;
});
return Promise.resolve().then(() => twitchPubSubManager.start()).then(() => {
console.log("Started.");
const stop = (error) => twitchPubSubManager.stop().then(() => {
if (error) {
console.error("Stopped.", error);
} else {
console.log("Stopped.");
}
return undefined;
});
console.log(`Online for ${MAX_LISTEN_TIME_MILLISECONDS} milliseconds.`);
// TODO: perform more work here.
return Promise.delay(MAX_LISTEN_TIME_MILLISECONDS).then(() => stop(), (error) => stop(error));
}).then(() => disconnect(), (error) => disconnect(error))
}).then(() => {
process.exit();
}, (error) => {
console.log("Error.", error);
process.exit(1);
});