fix heartbeats once and for all (#1016)

This commit is contained in:
Gus Caplan
2016-12-25 10:28:36 -06:00
committed by Amish Shah
parent fb6a8d1637
commit 2410fdf8d2
6 changed files with 25 additions and 18 deletions

View File

@@ -88,6 +88,8 @@ class WebSocketManager extends EventEmitter {
for (const event of client.options.disabledEvents) this.disabledEvents[event] = true;
this.first = true;
this.lastHeartbeatAck = true;
}
/**
@@ -122,6 +124,22 @@ class WebSocketManager extends EventEmitter {
}
}
heartbeat(normal) {
if (normal && !this.lastHeartbeatAck) {
this.ws.close(1007);
return;
}
this.client.emit('debug', 'Sending heartbeat');
this.client._pingTimestamp = Date.now();
this.client.ws.send({
op: Constants.OPCodes.HEARTBEAT,
d: this.sequence,
}, true);
this.lastHeartbeatAck = false;
}
/**
* Sends a packet to the gateway
* @param {Object} data An object that can be JSON stringified
@@ -167,6 +185,7 @@ class WebSocketManager extends EventEmitter {
*/
eventOpen() {
this.client.emit('debug', 'Connection to gateway opened');
this.lastHeartbeatAck = true;
if (this.status === Constants.Status.RECONNECTING) this._sendResume();
else this._sendNewIdentify();
}