mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 20:13:30 +01:00
src: Cleanup event listeners on WebSocket connections (#3681)
* src: Cleanup event listeners on WebSocket connections Should prevent #3641 from happening, as well as double connections on a shard * typings: Forgot to add the method
This commit is contained in:
@@ -245,6 +245,8 @@ class WebSocketManager extends EventEmitter {
|
|||||||
});
|
});
|
||||||
|
|
||||||
shard.on(ShardEvents.DESTROYED, () => {
|
shard.on(ShardEvents.DESTROYED, () => {
|
||||||
|
shard._cleanupConnection();
|
||||||
|
|
||||||
this.debug('Shard was destroyed but no WebSocket connection was present! Reconnecting...', shard);
|
this.debug('Shard was destroyed but no WebSocket connection was present! Reconnecting...', shard);
|
||||||
|
|
||||||
this.client.emit(Events.SHARD_RECONNECTING, shard.id);
|
this.client.emit(Events.SHARD_RECONNECTING, shard.id);
|
||||||
|
|||||||
@@ -208,11 +208,18 @@ class WebSocketShard extends EventEmitter {
|
|||||||
this.once(ShardEvents.INVALID_SESSION, onInvalid);
|
this.once(ShardEvents.INVALID_SESSION, onInvalid);
|
||||||
|
|
||||||
if (this.connection && this.connection.readyState === WebSocket.OPEN) {
|
if (this.connection && this.connection.readyState === WebSocket.OPEN) {
|
||||||
this.debug('Connection found, attempting an immediate identify.');
|
this.debug('An open connection was found, attempting an immediate identify.');
|
||||||
this.identify();
|
this.identify();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.connection) {
|
||||||
|
this.debug(`A connection was found. Cleaning up before continuing.
|
||||||
|
State: ${CONNECTION_STATE[this.connection.readyState]}`);
|
||||||
|
this._cleanupConnection();
|
||||||
|
this.connection.close(1000);
|
||||||
|
}
|
||||||
|
|
||||||
const wsQuery = { v: client.options.ws.version };
|
const wsQuery = { v: client.options.ws.version };
|
||||||
|
|
||||||
if (zlib) {
|
if (zlib) {
|
||||||
@@ -526,9 +533,9 @@ class WebSocketShard extends EventEmitter {
|
|||||||
} else if (!this.lastHeartbeatAcked) {
|
} else if (!this.lastHeartbeatAcked) {
|
||||||
this.debug(
|
this.debug(
|
||||||
`[${tag}] Didn't receive a heartbeat ack last time, assuming zombie connection. Destroying and reconnecting.
|
`[${tag}] Didn't receive a heartbeat ack last time, assuming zombie connection. Destroying and reconnecting.
|
||||||
Status : ${STATUS_KEYS[this.status]}
|
Status : ${STATUS_KEYS[this.status]}
|
||||||
Sequence : ${this.sequence}
|
Sequence : ${this.sequence}
|
||||||
Connection State: ${this.connection ? CONNECTION_STATE[this.connection.readyState] : 'No Connection??'}`
|
Connection State: ${this.connection ? CONNECTION_STATE[this.connection.readyState] : 'No Connection??'}`
|
||||||
);
|
);
|
||||||
this.destroy(4009);
|
this.destroy(4009);
|
||||||
return;
|
return;
|
||||||
@@ -629,7 +636,8 @@ class WebSocketShard extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
_send(data) {
|
_send(data) {
|
||||||
if (!this.connection || this.connection.readyState !== WebSocket.OPEN) {
|
if (!this.connection || this.connection.readyState !== WebSocket.OPEN) {
|
||||||
this.debug(`Tried to send packet ${JSON.stringify(data)} but no WebSocket is available!`);
|
this.debug(`Tried to send packet ${JSON.stringify(data)} but no WebSocket is available! Resetting the shard...`);
|
||||||
|
this.destroy(4000);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -667,6 +675,8 @@ class WebSocketShard extends EventEmitter {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
destroy(closeCode = 1000, cleanup = false) {
|
destroy(closeCode = 1000, cleanup = false) {
|
||||||
|
this.debug(`Destroying with close code ${closeCode}, attempting a reconnect: ${!cleanup}`);
|
||||||
|
|
||||||
this.setHeartbeatTimer(-1);
|
this.setHeartbeatTimer(-1);
|
||||||
this.setHelloTimeout(-1);
|
this.setHelloTimeout(-1);
|
||||||
|
|
||||||
@@ -696,6 +706,17 @@ class WebSocketShard extends EventEmitter {
|
|||||||
this.ratelimit.timer = null;
|
this.ratelimit.timer = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cleans up the WebSocket connection listeners.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
_cleanupConnection() {
|
||||||
|
this.connection.onopen =
|
||||||
|
this.connection.onclose =
|
||||||
|
this.connection.onerror =
|
||||||
|
this.connection.onmessage = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = WebSocketShard;
|
module.exports = WebSocketShard;
|
||||||
|
|||||||
1
typings/index.d.ts
vendored
1
typings/index.d.ts
vendored
@@ -1717,6 +1717,7 @@ declare module 'discord.js' {
|
|||||||
private _send(data: object): void;
|
private _send(data: object): void;
|
||||||
private processQueue(): void;
|
private processQueue(): void;
|
||||||
private destroy(closeCode: number): void;
|
private destroy(closeCode: number): void;
|
||||||
|
private _cleanupConnection(): void;
|
||||||
|
|
||||||
public send(data: object): void;
|
public send(data: object): void;
|
||||||
public on(event: 'ready', listener: () => void): this;
|
public on(event: 'ready', listener: () => void): this;
|
||||||
|
|||||||
Reference in New Issue
Block a user