Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support resume urls #8784

Merged
merged 8 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions packages/discord.js/src/client/websocket/WebSocketShard.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ class WebSocketShard extends EventEmitter {
* @private
*/
this.sessionId = null;


/**
* The resume url for this shard
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* The resume url for this shard
* The resume URL for this shard

* @type {?string}
* @private
*/
this.resumeUrl = null;
Deivu marked this conversation as resolved.
Show resolved Hide resolved

/**
* The previous heartbeat ping of the shard
* @type {number}
Expand Down Expand Up @@ -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;
Deivu marked this conversation as resolved.
Show resolved Hide resolved

return new Promise((resolve, reject) => {
const cleanup = () => {
this.removeListener(WebSocketShardEvents.Close, onClose);
Expand Down Expand Up @@ -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}.`);
Deivu marked this conversation as resolved.
Show resolved Hide resolved
this.lastHeartbeatAcked = true;
this.sendHeartbeat('ReadyHeartbeat');
break;
Expand Down
1 change: 1 addition & 0 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2994,6 +2994,7 @@ export class WebSocketShard extends EventEmitter {
private sequence: number;
private closeSequence: number;
private sessionId: string | null;
private resumeUrl: string | null;
Deivu marked this conversation as resolved.
Show resolved Hide resolved
private lastPingTimestamp: number;
private lastHeartbeatAcked: boolean;
private readonly ratelimit: {
Expand Down