mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-14 10:33:30 +01:00
fix heartbeats once and for all (#1016)
This commit is contained in:
@@ -416,7 +416,7 @@ class Client extends EventEmitter {
|
|||||||
_pong(startTime) {
|
_pong(startTime) {
|
||||||
this.pings.unshift(Date.now() - startTime);
|
this.pings.unshift(Date.now() - startTime);
|
||||||
if (this.pings.length > 3) this.pings.length = 3;
|
if (this.pings.length > 3) this.pings.length = 3;
|
||||||
this.clearTimeout(this._ackTimeout);
|
this.ws.lastHeartbeatAck = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
_setPresence(id, presence) {
|
_setPresence(id, presence) {
|
||||||
|
|||||||
@@ -48,22 +48,7 @@ class ClientManager {
|
|||||||
* @param {number} time The interval in milliseconds at which heartbeat packets should be sent
|
* @param {number} time The interval in milliseconds at which heartbeat packets should be sent
|
||||||
*/
|
*/
|
||||||
setupKeepAlive(time) {
|
setupKeepAlive(time) {
|
||||||
this.heartbeatInterval = this.client.setInterval(this.ping.bind(this), time);
|
this.heartbeatInterval = this.client.setInterval(() => this.client.ws.heartbeat(true), time);
|
||||||
}
|
|
||||||
|
|
||||||
ping() {
|
|
||||||
this.client.emit('debug', 'Sending heartbeat');
|
|
||||||
this.client._pingTimestamp = Date.now();
|
|
||||||
this.client.ws.send({
|
|
||||||
op: Constants.OPCodes.HEARTBEAT,
|
|
||||||
d: this.client.ws.sequence,
|
|
||||||
}, true);
|
|
||||||
|
|
||||||
const lastPing = this.client.ping;
|
|
||||||
|
|
||||||
this.client._ackTimeout = this.client.setTimeout(() => {
|
|
||||||
this.client.ws.ws.close(1005);
|
|
||||||
}, lastPing ? lastPing * 20 : 20e3);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy() {
|
destroy() {
|
||||||
|
|||||||
@@ -88,6 +88,8 @@ class WebSocketManager extends EventEmitter {
|
|||||||
for (const event of client.options.disabledEvents) this.disabledEvents[event] = true;
|
for (const event of client.options.disabledEvents) this.disabledEvents[event] = true;
|
||||||
|
|
||||||
this.first = 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
|
* 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
|
||||||
@@ -167,6 +185,7 @@ class WebSocketManager extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
eventOpen() {
|
eventOpen() {
|
||||||
this.client.emit('debug', 'Connection to gateway opened');
|
this.client.emit('debug', 'Connection to gateway opened');
|
||||||
|
this.lastHeartbeatAck = true;
|
||||||
if (this.status === Constants.Status.RECONNECTING) this._sendResume();
|
if (this.status === Constants.Status.RECONNECTING) this._sendResume();
|
||||||
else this._sendNewIdentify();
|
else this._sendNewIdentify();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -84,6 +84,7 @@ class WebSocketPacketManager {
|
|||||||
|
|
||||||
if (packet.op === Constants.OPCodes.HEARTBEAT_ACK) {
|
if (packet.op === Constants.OPCodes.HEARTBEAT_ACK) {
|
||||||
this.ws.client._pong(this.ws.client._pingTimestamp);
|
this.ws.client._pong(this.ws.client._pingTimestamp);
|
||||||
|
this.ws.lastHeartbeatAck = true;
|
||||||
this.ws.client.emit('debug', 'Heartbeat acknowledged');
|
this.ws.client.emit('debug', 'Heartbeat acknowledged');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ class GuildMembersChunkHandler extends AbstractHandler {
|
|||||||
|
|
||||||
guild._checkChunks();
|
guild._checkChunks();
|
||||||
client.emit(Constants.Events.GUILD_MEMBERS_CHUNK, members);
|
client.emit(Constants.Events.GUILD_MEMBERS_CHUNK, members);
|
||||||
|
|
||||||
|
client.ws.lastHeartbeatAck = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ class ReadyHandler extends AbstractHandler {
|
|||||||
const client = this.packetManager.client;
|
const client = this.packetManager.client;
|
||||||
const data = packet.d;
|
const data = packet.d;
|
||||||
|
|
||||||
client.manager.ping();
|
client.ws.heartbeat();
|
||||||
|
|
||||||
const clientUser = new ClientUser(client, data.user);
|
const clientUser = new ClientUser(client, data.user);
|
||||||
client.user = clientUser;
|
client.user = clientUser;
|
||||||
|
|||||||
Reference in New Issue
Block a user