Add client ping stuff

This commit is contained in:
Schuyler Cebulskie
2016-12-02 20:58:19 -05:00
parent 2488e1a00f
commit 58c7c2e7b8
3 changed files with 26 additions and 1 deletions

View File

@@ -148,6 +148,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();
@@ -174,6 +181,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>}
@@ -392,6 +408,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);