-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgateway_client.js
143 lines (128 loc) · 3.81 KB
/
gateway_client.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
import { log } from "../util/log.js";
import { Queue } from "../util/queue.js";
import { ShardClient } from "./shard_client.js";
export class GatewayClient {
#token;
options;
queue;
shards = new Map();
constructor(token, options) {
this.#token = token;
this.options = options;
this.queue = new Queue(options.maxConcurrency);
}
connect() {
const { firstShard = 0, shards = 1, lastShard = shards, url } = this.options;
log("DBG", `Connecting ${lastShard - firstShard}/${shards} shards (${firstShard}-${lastShard - 1}) to ${url}`);
for (let i = firstShard; i < lastShard; i++) {
const shard = new ShardClient(i, {
close: (event, s) => this.shardClose(event, s),
message: (event, s) => this.shardMessage(event, s),
open: (_, s) => this.identifyOrResumeShard(s),
});
this.connectShard(shard);
this.shards.set(i, shard);
}
}
connectShard(shard) {
const url = `${shard.resumeGatewayUrl ?? this.options.url}?encoding=json&v=${this.options?.version ?? "10"}`;
log("DBG", `Shard ${shard.id}: Connecting to ${url}`);
shard.connect(url);
}
disconnect(code, reason) {
for (const shard of this.shards.values()) {
shard.disconnect(code, reason);
}
}
identifyOrResumeShard(shard) {
if (shard.sessionId !== undefined) {
log("DBG", `Shard ${shard.id}: Resuming...`);
shard.resume(this.#token);
return;
}
this.queue.add().then(() => {
log("DBG", `Shard ${shard.id}: Identifying...`);
shard.identify({
properties: {
browser: "whirlybird",
device: "whirlybird",
os: "whirlybird",
},
shard: [shard.id, this.options.shards ?? 1],
token: this.#token,
...this.options.identify,
});
});
}
shardClose(event, shard) {
log("WRN", `Shard ${shard.id}: Closed with code ${event.code} - ${event.reason}`);
clearInterval(shard.heartbeatInterval);
shard.heartbeatInterval = undefined;
shard.ws = undefined;
if (event.code > 3899 && event.code < 4000) {
return;
}
switch (event.code) {
case 4004:
case 4010:
case 4011:
case 4012:
case 4013:
case 4014: {
throw new Error(event.reason);
}
}
setTimeout(() => this.connectShard(shard), 5_000);
}
shardMessage(event, shard) {
const data = JSON.parse(event.data);
switch (data.op) {
case 0: {
shard.seq = data.s;
switch (data.t) {
case "READY": {
log("INF", `Shard ${shard.id}: Ready with session ${data.d.session_id}`);
shard.ready = true;
shard.resumeGatewayUrl = data.d.resume_gateway_url;
shard.sessionId = data.d.session_id;
this.queue.update();
this.queue.next();
break;
}
case "RESUMED": {
log("INF", `shard ${shard.id}: Resumed session ${shard.sessionId}`);
shard.ready = true;
break;
}
}
this.options.handleEvent(data.t, data.d, shard);
break;
}
case 7: {
shard.disconnect(3000, "Request by Discord to reconnect");
break;
}
case 9: {
log("WRN", `Shard ${shard.id}: Encountered an invalid session`);
if (data.d === false) {
shard.ready = false;
shard.resumeGatewayUrl = undefined;
shard.seq = null;
shard.sessionId = undefined;
this.queue.update();
this.queue.next();
}
this.identifyOrResumeShard(shard);
break;
}
case 10: {
shard.heartbeatInterval = setInterval(() => shard.heartbeat(), data.d.heartbeat_interval);
break;
}
case 11: {
shard.latency = Date.now() - shard.heartbeatAt;
break;
}
}
}
}