mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
feat: support resume urls (#8784)
Co-authored-by: Almeida <almeidx@pm.me>
This commit is contained in:
@@ -64,6 +64,13 @@ class WebSocketShard extends EventEmitter {
|
||||
*/
|
||||
this.sessionId = null;
|
||||
|
||||
/**
|
||||
* The resume url for this shard
|
||||
* @type {?string}
|
||||
* @private
|
||||
*/
|
||||
this.resumeURL = null;
|
||||
|
||||
/**
|
||||
* The previous heartbeat ping of the shard
|
||||
* @type {number}
|
||||
@@ -193,12 +200,14 @@ class WebSocketShard extends EventEmitter {
|
||||
* or reject if we couldn't connect
|
||||
*/
|
||||
connect() {
|
||||
const { gateway, client } = this.manager;
|
||||
const { client } = this.manager;
|
||||
|
||||
if (this.connection?.readyState === WebSocket.OPEN && this.status === Status.Ready) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
const gateway = this.resumeURL ?? this.manager.gateway;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const cleanup = () => {
|
||||
this.removeListener(WebSocketShardEvents.Close, onClose);
|
||||
@@ -416,9 +425,10 @@ class WebSocketShard extends EventEmitter {
|
||||
this.emit(WebSocketShardEvents.Ready);
|
||||
|
||||
this.sessionId = packet.d.session_id;
|
||||
this.resumeURL = packet.d.resume_gateway_url;
|
||||
this.expectedGuilds = new Set(packet.d.guilds.map(d => d.id));
|
||||
this.status = Status.WaitingForGuilds;
|
||||
this.debug(`[READY] Session ${this.sessionId}.`);
|
||||
this.debug(`[READY] Session ${this.sessionId} | Resume url ${this.resumeURL}.`);
|
||||
this.lastHeartbeatAcked = true;
|
||||
this.sendHeartbeat('ReadyHeartbeat');
|
||||
break;
|
||||
@@ -847,10 +857,11 @@ class WebSocketShard extends EventEmitter {
|
||||
// Step 4: Cache the old sequence (use to attempt a resume)
|
||||
if (this.sequence !== -1) this.closeSequence = this.sequence;
|
||||
|
||||
// Step 5: Reset the sequence and session id if requested
|
||||
// Step 5: Reset the sequence, resume url and session id if requested
|
||||
if (reset) {
|
||||
this.sequence = -1;
|
||||
this.sessionId = null;
|
||||
this.resumeURL = null;
|
||||
}
|
||||
|
||||
// Step 6: reset the rate limit data
|
||||
|
||||
1
packages/discord.js/typings/index.d.ts
vendored
1
packages/discord.js/typings/index.d.ts
vendored
@@ -2995,6 +2995,7 @@ export class WebSocketShard extends EventEmitter {
|
||||
private sequence: number;
|
||||
private closeSequence: number;
|
||||
private sessionId: string | null;
|
||||
private resumeURL: string | null;
|
||||
private lastPingTimestamp: number;
|
||||
private lastHeartbeatAcked: boolean;
|
||||
private readonly ratelimit: {
|
||||
|
||||
Reference in New Issue
Block a user