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( this.debug(
`Received HELLO. Heartbeat Interval: ${this.heartbeatInterval}` `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({ this.sendHeartbeat()
op: GatewayOpcodes.HEARTBEAT, this.heartbeatIntervalID = setInterval(() => {
d: this.sequenceID ?? null this.heartbeat()
})
this.lastPingTimestamp = Date.now()
}, this.heartbeatInterval) }, this.heartbeatInterval)
if (!this.initialized) { if (!this.initialized) {
@ -305,6 +294,29 @@ class Gateway {
d: data 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 export type GatewayEventHandler = (gateway: Gateway, d: any) => void