This commit is contained in:
Travis CI 2016-12-03 01:59:15 +00:00
parent 5a3b7d2621
commit 517ea9a716
2 changed files with 30 additions and 5 deletions

View file

@ -11048,6 +11048,13 @@ class Client extends EventEmitter {
*/
this.readyAt = null;
/**
* The previous heartbeat pings of the websocket (most recent first, limited to three elements)
* @type {number[]}
*/
this.pings = [];
this._pingTimestamp = 0;
this._timeouts = new Set();
this._intervals = new Set();
@ -11074,6 +11081,15 @@ class Client extends EventEmitter {
return this.readyAt ? Date.now() - this.readyAt : null;
}
/**
* The average heartbeat ping of the websocket
* @type {number}
* @readonly
*/
get ping() {
return this.pings.reduce((prev, p) => prev + p, 0) / this.pings.length;
}
/**
* Returns a collection, mapping guild ID to voice connections.
* @type {Collection<string, VoiceConnection>}
@ -11292,6 +11308,11 @@ class Client extends EventEmitter {
this._intervals.delete(interval);
}
_pong(startTime) {
this.pings.unshift(Date.now() - startTime);
if (this.pings.length > 3) this.pings.length = 3;
}
_setPresence(id, presence) {
if (this.presences.get(id)) {
this.presences.get(id).update(presence);
@ -18701,6 +18722,7 @@ class ClientManager {
setupKeepAlive(time) {
this.heartbeatInterval = this.client.setInterval(() => {
this.client.emit('debug', 'Sending heartbeat');
this.client._pingTimestamp = Date.now();
this.client.ws.send({
op: Constants.OPCodes.HEARTBEAT,
d: this.client.ws.sequence,
@ -21058,7 +21080,10 @@ class WebSocketPacketManager {
return false;
}
if (packet.op === Constants.OPCodes.HEARTBEAT_ACK) this.ws.client.emit('debug', 'Heartbeat acknowledged');
if (packet.op === Constants.OPCodes.HEARTBEAT_ACK) {
this.ws.client._pong(this.ws.client._pingTimestamp);
this.ws.client.emit('debug', 'Heartbeat acknowledged');
}
if (this.ws.status === Constants.Status.RECONNECTING) {
this.ws.reconnecting = false;

File diff suppressed because one or more lines are too long