-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
eventsub.js
208 lines (171 loc) · 7.09 KB
/
eventsub.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
class initSocket {
counter = 0
closeCodes = {
4000: 'Internal Server Error',
4001: 'Client sent inbound traffic',
4002: 'Client failed ping-pong',
4003: 'Connection unused',
4004: 'Reconnect grace time expired',
4005: 'Network Timeout',
4006: 'Network error',
4007: 'Invalid Reconnect'
}
constructor(connect) {
this._events = {};
if (connect) {
this.connect();
}
}
connect(url, is_reconnect) {
this.eventsub = {};
this.counter++;
url = url ? url : 'wss://eventsub.wss.twitch.tv/ws';
is_reconnect = is_reconnect ? is_reconnect : false;
log(`Connecting to ${url}|${is_reconnect}`);
this.eventsub = new WebSocket(url);
this.eventsub.is_reconnecting = is_reconnect;
this.eventsub.counter = this.counter;
this.eventsub.addEventListener('open', () => {
log(`Opened Connection to Twitch`);
});
// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close_event
// https://github.com/Luka967/websocket-close-codes
this.eventsub.addEventListener('close', (close) => {
console.log('EventSub close', close, this.eventsub);
log(`${this.eventsub.twitch_websocket_id}/${this.eventsub.counter} Connection Closed: ${close.code} Reason - ${this.closeCodes[close.code]}`);
if (!this.eventsub.is_reconnecting) {
log(`${this.eventsub.twitch_websocket_id}/${this.eventsub.counter} Is not reconnecting, auto reconnect`);
//new initSocket();
this.connect();
}
if (close.code == 1006) {
// do a single retry
this.eventsub.is_reconnecting = true;
}
});
// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/error_event
this.eventsub.addEventListener('error', (err) => {
console.log(err);
log(`${this.eventsub.twitch_websocket_id}/${this.eventsub.counter} Connection Error`);
});
// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/message_event
this.eventsub.addEventListener('message', (message) => {
//log('Message');
//console.log(this.eventsub.counter, message);
let { data } = message;
data = JSON.parse(data);
let { metadata, payload } = data;
let { message_id, message_type, message_timestamp } = metadata;
//log(`Recv ${message_id} - ${message_type}`);
switch (message_type) {
case 'session_welcome':
let { session } = payload;
let { id, keepalive_timeout_seconds } = session;
log(`${this.eventsub.counter} This is Socket ID ${id}`);
this.eventsub.twitch_websocket_id = id;
log(`${this.eventsub.counter} This socket declared silence as ${keepalive_timeout_seconds} seconds`);
if (!this.eventsub.is_reconnecting) {
log('Dirty disconnect or first spawn');
this.emit('connected', id);
// now you would spawn your topics
} else {
this.emit('reconnected', id);
// no need to spawn topics as carried over
}
this.silence(keepalive_timeout_seconds);
break;
case 'session_keepalive':
//log(`Recv KeepAlive - ${message_type}`);
this.emit('session_keepalive');
this.silence();
break;
case 'notification':
//console.log('notification', metadata, payload);
//log(`${this.eventsub.twitch_websocket_id}/${this.eventsub.counter} Recv notification`);// ${JSON.stringify(payload)}`);
let { subscription, event } = payload;
let { type } = subscription;
this.emit('notification', { metadata, payload });
this.emit(type, { metadata, payload });
this.silence();
break;
case 'session_reconnect':
this.eventsub.is_reconnecting = true;
let reconnect_url = payload.session.reconnect_url;
console.log('Connect to new url', reconnect_url);
log(`${this.eventsub.twitch_websocket_id}/${this.eventsub.counter} Reconnect request ${reconnect_url}`)
//this.eventsub.close();
//new initSocket(reconnect_url, true);
this.connect(reconnect_url, true);
break;
case 'websocket_disconnect':
log(`${this.eventsub.counter} Recv Disconnect`);
console.log('websocket_disconnect', payload);
break;
case 'revocation':
log(`${this.eventsub.counter} Recv Topic Revocation`);
console.log('revocation', payload);
this.emit('revocation', { metadata, payload });
break;
default:
console.log(`${this.eventsub.counter} unexpected`, metadata, payload);
break;
}
});
}
trigger() {
// this function lets you test the disconnect on send method
this.eventsub.send('cat');
}
close() {
this.eventsub.close();
}
silenceHandler = false;
silenceTime = 10;// default per docs is 10 so set that as a good default
silence(keepalive_timeout_seconds) {
if (keepalive_timeout_seconds) {
this.silenceTime = keepalive_timeout_seconds;
this.silenceTime++;// add a little window as it's too anal
}
clearTimeout(this.silenceHandler);
this.silenceHandler = setTimeout(() => {
this.emit('session_silenced');// -> self reconnecting
this.close();// close it and let it self loop
}, (this.silenceTime * 1000));
}
on(name, listener) {
if (!this._events[name]) {
this._events[name] = [];
}
this._events[name].push(listener);
}
emit(name, data) {
if (!this._events[name]) {
return;
}
const fireCallbacks = (callback) => {
callback(data);
};
this._events[name].forEach(fireCallbacks);
}
}
function log(msg) {
if (!document.getElementById('log')) {
return;
}
let div = document.createElement('div');
document.getElementById('log').prepend(div);
let tim = document.createElement('span');
div.append(tim);
let t = [
new Date().getHours(),
new Date().getMinutes(),
new Date().getSeconds()
]
t.forEach((v,i) => {
t[i] = v < 10 ? '0'+v : v;
});
tim.textContent = t.join(':');
let sp = document.createElement('span');
div.append(sp);
sp.textContent = msg;
}