Immediately Heartbeat on HELLO for non-zero ping initially

This commit is contained in:
DjDeveloperr 2020-11-06 13:33:28 +05:30
parent f6eaa739ab
commit e40b7bd544

View file

@ -76,21 +76,10 @@ class Gateway {
this.debug(
`Received HELLO. Heartbeat Interval: ${this.heartbeatInterval}`
)
this.heartbeatIntervalID = setInterval(() => {
if (this.heartbeatServerResponded) {
this.heartbeatServerResponded = false
} else {
clearInterval(this.heartbeatIntervalID)
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.reconnect()
return
}
this.send({
op: GatewayOpcodes.HEARTBEAT,
d: this.sequenceID ?? null
})
this.lastPingTimestamp = Date.now()
this.sendHeartbeat()
this.heartbeatIntervalID = setInterval(() => {
this.heartbeat()
}, this.heartbeatInterval)
if (!this.initialized) {
@ -305,6 +294,29 @@ class Gateway {
d: data
})
}
sendHeartbeat() {
const payload = {
op: GatewayOpcodes.HEARTBEAT,
d: this.sequenceID ?? null
};
this.send(payload)
this.lastPingTimestamp = Date.now()
}
heartbeat() {
if (this.heartbeatServerResponded) {
this.heartbeatServerResponded = false
} else {
clearInterval(this.heartbeatIntervalID)
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.reconnect()
return
}
this.sendHeartbeat()
}
}
export type GatewayEventHandler = (gateway: Gateway, d: any) => void