feat(WebSocketShard): support new resume url (2)

clean code & fix
This commit is contained in:
March 7th 2023-01-27 11:45:47 +07:00
parent cd29293f4e
commit 107f9c564b

View File

@ -64,6 +64,7 @@ class WebSocketShard extends EventEmitter {
/** /**
* URL to use when resuming * URL to use when resuming
* @type {?string} * @type {?string}
* @private
*/ */
this.resumeURL = null; this.resumeURL = null;
@ -196,7 +197,9 @@ class WebSocketShard extends EventEmitter {
* or reject if we couldn't connect * or reject if we couldn't connect
*/ */
connect() { connect() {
const { gateway, client } = this.manager; const { client } = this.manager;
const gateway = this.resumeURL ?? this.manager.gateway;
if (this.connection?.readyState === WebSocket.OPEN && this.status === Status.READY) { if (this.connection?.readyState === WebSocket.OPEN && this.status === Status.READY) {
return Promise.resolve(); return Promise.resolve();
@ -263,7 +266,7 @@ class WebSocketShard extends EventEmitter {
this.debug( this.debug(
`[CONNECT] `[CONNECT]
Gateway : ${this.resumeURL ?? gateway} Gateway : ${gateway}
Version : ${client.options.ws.version} Version : ${client.options.ws.version}
Encoding : ${WebSocket.encoding} Encoding : ${WebSocket.encoding}
Compression: ${zlib ? 'zlib-stream' : 'none'} Compression: ${zlib ? 'zlib-stream' : 'none'}
@ -282,7 +285,7 @@ class WebSocketShard extends EventEmitter {
this.debug(`Using proxy ${client.options.proxy}`, args); this.debug(`Using proxy ${client.options.proxy}`, args);
} }
// Adding a handshake timeout to just make sure no zombie connection appears. // Adding a handshake timeout to just make sure no zombie connection appears.
const ws = (this.connection = WebSocket.create(this.resumeURL ?? gateway, wsQuery, args)); const ws = (this.connection = WebSocket.create(gateway, wsQuery, args));
ws.onopen = this.onOpen.bind(this); ws.onopen = this.onOpen.bind(this);
ws.onmessage = this.onMessage.bind(this); ws.onmessage = this.onMessage.bind(this);
ws.onerror = this.onError.bind(this); ws.onerror = this.onError.bind(this);
@ -863,8 +866,9 @@ class WebSocketShard extends EventEmitter {
// Step 4: Cache the old sequence (use to attempt a resume) // Step 4: Cache the old sequence (use to attempt a resume)
if (this.sequence !== -1) this.closeSequence = this.sequence; 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) { if (reset) {
this.resumeURL = null;
this.sequence = -1; this.sequence = -1;
this.sessionId = null; this.sessionId = null;
} }