mirror of
https://github.com/danbulant/discord.js
synced 2026-06-20 23:21:04 +00:00
Fix resuming sessions (fixes #699)
This commit is contained in:
parent
761b8cfb8d
commit
534d7af8c3
4 changed files with 28 additions and 6 deletions
File diff suppressed because one or more lines are too long
|
|
@ -58,16 +58,20 @@ class WebSocketManager extends EventEmitter {
|
|||
* @type {?WebSocket}
|
||||
*/
|
||||
this.ws = null;
|
||||
|
||||
this.first = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects the client to a given gateway
|
||||
* @param {string} gateway The gateway to connect to
|
||||
*/
|
||||
connect(gateway) {
|
||||
_connect(gateway) {
|
||||
this.client.emit('debug', `Connecting to gateway ${gateway}`);
|
||||
this.normalReady = false;
|
||||
this.status = Constants.Status.CONNECTING;
|
||||
if (this.status !== Constants.Status.RECONNECTING) {
|
||||
this.status = Constants.Status.CONNECTING;
|
||||
}
|
||||
this.ws = new WebSocket(gateway);
|
||||
this.ws.onopen = () => this.eventOpen();
|
||||
this.ws.onclose = (d) => this.eventClose(d);
|
||||
|
|
@ -77,6 +81,15 @@ class WebSocketManager extends EventEmitter {
|
|||
this._remaining = 3;
|
||||
}
|
||||
|
||||
connect(gateway) {
|
||||
if (this.first) {
|
||||
this._connect(gateway);
|
||||
this.first = false;
|
||||
} else {
|
||||
this.client.setTimeout(() => this._connect(gateway), 5500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a packet to the gateway
|
||||
* @param {Object} data An object that can be JSON stringified
|
||||
|
|
@ -125,7 +138,7 @@ class WebSocketManager extends EventEmitter {
|
|||
*/
|
||||
eventOpen() {
|
||||
this.client.emit('debug', 'Connection to gateway opened');
|
||||
if (this.reconnecting) this._sendResume();
|
||||
if (this.status === Constants.Status.RECONNECTING) this._sendResume();
|
||||
else this._sendNewIdentify();
|
||||
}
|
||||
|
||||
|
|
@ -133,6 +146,11 @@ class WebSocketManager extends EventEmitter {
|
|||
* Sends a gateway resume packet, in cases of unexpected disconnections.
|
||||
*/
|
||||
_sendResume() {
|
||||
if (!this.sessionID) {
|
||||
this._sendNewIdentify();
|
||||
return;
|
||||
}
|
||||
this.client.emit('debug', 'identifying as resumed session');
|
||||
const payload = {
|
||||
token: this.client.token,
|
||||
session_id: this.sessionID,
|
||||
|
|
@ -155,6 +173,7 @@ class WebSocketManager extends EventEmitter {
|
|||
if (this.client.options.shard_count > 0) {
|
||||
payload.shard = [Number(this.client.options.shard_id), Number(this.client.options.shard_count)];
|
||||
}
|
||||
this.client.emit('debug', 'identifying as new session');
|
||||
this.send({
|
||||
op: Constants.OPCodes.IDENTIFY,
|
||||
d: payload,
|
||||
|
|
|
|||
|
|
@ -72,11 +72,12 @@ class WebSocketPacketManager {
|
|||
}
|
||||
|
||||
if (packet.op === Constants.OPCodes.INVALID_SESSION) {
|
||||
this.ws.sessionID = null;
|
||||
this.ws._sendNewIdentify();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.ws.reconnecting) {
|
||||
if (this.ws.status === Constants.Status.RECONNECTING) {
|
||||
this.ws.reconnecting = false;
|
||||
this.ws.checkIfReady();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,9 @@ client.on('channelCreate', channel => {
|
|||
console.log(`made ${channel.name}`);
|
||||
});
|
||||
|
||||
client.on('debug', console.log);
|
||||
client.on('debug', m => console.log('debug', m));
|
||||
client.on('error', m => console.log('debug', m));
|
||||
client.on('reconnecting', m => console.log('debug', m));
|
||||
|
||||
client.on('message', message => {
|
||||
if (true) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue