Fix resuming sessions (fixes #699)

This commit is contained in:
Amish Shah
2016-09-25 15:03:48 +01:00
parent 761b8cfb8d
commit 534d7af8c3
4 changed files with 28 additions and 6 deletions

File diff suppressed because one or more lines are too long

View File

@@ -58,16 +58,20 @@ class WebSocketManager extends EventEmitter {
* @type {?WebSocket} * @type {?WebSocket}
*/ */
this.ws = null; this.ws = null;
this.first = true;
} }
/** /**
* Connects the client to a given gateway * Connects the client to a given gateway
* @param {string} gateway The gateway to connect to * @param {string} gateway The gateway to connect to
*/ */
connect(gateway) { _connect(gateway) {
this.client.emit('debug', `Connecting to gateway ${gateway}`); this.client.emit('debug', `Connecting to gateway ${gateway}`);
this.normalReady = false; 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 = new WebSocket(gateway);
this.ws.onopen = () => this.eventOpen(); this.ws.onopen = () => this.eventOpen();
this.ws.onclose = (d) => this.eventClose(d); this.ws.onclose = (d) => this.eventClose(d);
@@ -77,6 +81,15 @@ class WebSocketManager extends EventEmitter {
this._remaining = 3; 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 * Sends a packet to the gateway
* @param {Object} data An object that can be JSON stringified * @param {Object} data An object that can be JSON stringified
@@ -125,7 +138,7 @@ class WebSocketManager extends EventEmitter {
*/ */
eventOpen() { eventOpen() {
this.client.emit('debug', 'Connection to gateway opened'); this.client.emit('debug', 'Connection to gateway opened');
if (this.reconnecting) this._sendResume(); if (this.status === Constants.Status.RECONNECTING) this._sendResume();
else this._sendNewIdentify(); else this._sendNewIdentify();
} }
@@ -133,6 +146,11 @@ class WebSocketManager extends EventEmitter {
* Sends a gateway resume packet, in cases of unexpected disconnections. * Sends a gateway resume packet, in cases of unexpected disconnections.
*/ */
_sendResume() { _sendResume() {
if (!this.sessionID) {
this._sendNewIdentify();
return;
}
this.client.emit('debug', 'identifying as resumed session');
const payload = { const payload = {
token: this.client.token, token: this.client.token,
session_id: this.sessionID, session_id: this.sessionID,
@@ -155,6 +173,7 @@ class WebSocketManager extends EventEmitter {
if (this.client.options.shard_count > 0) { if (this.client.options.shard_count > 0) {
payload.shard = [Number(this.client.options.shard_id), Number(this.client.options.shard_count)]; payload.shard = [Number(this.client.options.shard_id), Number(this.client.options.shard_count)];
} }
this.client.emit('debug', 'identifying as new session');
this.send({ this.send({
op: Constants.OPCodes.IDENTIFY, op: Constants.OPCodes.IDENTIFY,
d: payload, d: payload,

View File

@@ -72,11 +72,12 @@ class WebSocketPacketManager {
} }
if (packet.op === Constants.OPCodes.INVALID_SESSION) { if (packet.op === Constants.OPCodes.INVALID_SESSION) {
this.ws.sessionID = null;
this.ws._sendNewIdentify(); this.ws._sendNewIdentify();
return false; return false;
} }
if (this.ws.reconnecting) { if (this.ws.status === Constants.Status.RECONNECTING) {
this.ws.reconnecting = false; this.ws.reconnecting = false;
this.ws.checkIfReady(); this.ws.checkIfReady();
} }

View File

@@ -18,7 +18,9 @@ client.on('channelCreate', channel => {
console.log(`made ${channel.name}`); 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 => { client.on('message', message => {
if (true) { if (true) {