From 9a7706fcf46551fc8b1c5d8c0bfb27cba77a0b5f Mon Sep 17 00:00:00 2001 From: March 7th <71698422+aiko-chan-ai@users.noreply.github.com> Date: Fri, 6 Jan 2023 19:05:03 +0700 Subject: [PATCH] feat(WebSocketShard): support new resume url --- src/client/websocket/WebSocketShard.js | 12 +++++++++--- typings/index.d.ts | 1 + 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/client/websocket/WebSocketShard.js b/src/client/websocket/WebSocketShard.js index 776e25b..763ba6b 100644 --- a/src/client/websocket/WebSocketShard.js +++ b/src/client/websocket/WebSocketShard.js @@ -62,6 +62,11 @@ class WebSocketShard extends EventEmitter { */ this.sessionId = null; + /** + * URL to use when resuming + */ + this.resumeURL = null; + /** * The previous heartbeat ping of the shard * @type {number} @@ -258,7 +263,7 @@ class WebSocketShard extends EventEmitter { this.debug( `[CONNECT] - Gateway : ${gateway} + Gateway : ${this.resumeURL ?? gateway} Version : ${client.options.ws.version} Encoding : ${WebSocket.encoding} Compression: ${zlib ? 'zlib-stream' : 'none'} @@ -276,7 +281,7 @@ class WebSocketShard extends EventEmitter { this.debug(`Using proxy ${client.options.proxy}`, args); } // Adding a handshake timeout to just make sure no zombie connection appears. - const ws = (this.connection = WebSocket.create(gateway, wsQuery, args)); + const ws = (this.connection = WebSocket.create(this.resumeURL ?? gateway, wsQuery, args)); ws.onopen = this.onOpen.bind(this); ws.onmessage = this.onMessage.bind(this); ws.onerror = this.onError.bind(this); @@ -425,9 +430,10 @@ class WebSocketShard extends EventEmitter { this.emit(ShardEvents.READY); this.sessionId = packet.d.session_id; + this.resumeURL = packet.d.resume_gateway_url; this.expectedGuilds = new Set(packet.d.guilds.filter(d => d?.unavailable == true).map(d => d.id)); this.status = Status.WAITING_FOR_GUILDS; - this.debug(`[READY] Session ${this.sessionId}.`); + this.debug(`[READY] Session ${this.sessionId} | ResumeURL ${this.resumeURL}.`); this.lastHeartbeatAcked = true; this.sendHeartbeat('ReadyHeartbeat'); break; diff --git a/typings/index.d.ts b/typings/index.d.ts index b1015b6..b5233cc 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -3336,6 +3336,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 ratelimit: { queue: unknown[]; total: number; remaining: number; time: 60e3; timer: NodeJS.Timeout | null };