mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 16:43:31 +01:00
Add client ping stuff
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -50,6 +50,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,
|
||||
|
||||
@@ -82,7 +82,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;
|
||||
|
||||
Reference in New Issue
Block a user