mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-02 00:40:07 +00:00
* fix: check new types idea * fix: type errors * fix: new style * fix: more cleanup * fix: more cleanup * fix: cleanup audit logs * fix: cleanup stickers * fix: cleanup integrations * fix: more cleanup * fix: organize into 1 place * fix: few errors * fix: some broken import fixes * fix: quite a lot of fixes across the board * fix: more fixes for broken imports * fix: more fixes for broken imports * fix: handler imports * fix: all remaining import errors * fix: more errors needing fixes * fix: clearing up transformers * fix: few moer types * fix: more cleanup of extra types * fix: fmt * fix: cleanup discordeno file * Nuke Base Types (#2102) * fix: cleanup snake stuff * convert camelCase to snake_case (#2103) * fix: add camelize * fix: finalize remaining errors * fix: imports in test Co-authored-by: LTS20050703 <87189679+lts20050703@users.noreply.github.com>
61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
import { GatewayOpcodes } from "../types/shared.ts";
|
|
import { delay } from "../util/utils.ts";
|
|
import { GatewayManager } from "./gateway_manager.ts";
|
|
|
|
export async function heartbeat(gateway: GatewayManager, shardId: number, interval: number) {
|
|
gateway.debug("GW HEARTBEATING_STARTED", { shardId, interval });
|
|
|
|
const shard = gateway.shards.get(shardId);
|
|
if (!shard) return;
|
|
|
|
gateway.debug("GW HEARTBEATING_DETAILS", { shardId, interval, shard });
|
|
|
|
// The first heartbeat is special so we send it without set Interval: https://discord.com/developers/docs/topics/gateway#heartbeating
|
|
await delay(Math.floor(shard.heartbeat.interval * Math.random()));
|
|
|
|
if (shard.ws.readyState !== WebSocket.OPEN) return;
|
|
|
|
shard.ws.send(
|
|
JSON.stringify({
|
|
op: GatewayOpcodes.Heartbeat,
|
|
d: shard.previousSequenceNumber,
|
|
}),
|
|
);
|
|
|
|
shard.heartbeat.keepAlive = true;
|
|
shard.heartbeat.acknowledged = false;
|
|
shard.heartbeat.lastSentAt = Date.now();
|
|
shard.heartbeat.interval = interval;
|
|
|
|
shard.heartbeat.intervalId = setInterval(async () => {
|
|
gateway.debug("GW DEBUG", `Running setInterval in heartbeat file. Shard: ${shardId}`);
|
|
const currentShard = gateway.shards.get(shardId);
|
|
if (!currentShard) return;
|
|
|
|
gateway.debug("GW HEARTBEATING", { shardId, shard: currentShard });
|
|
|
|
if (currentShard.ws.readyState === WebSocket.CLOSED || !currentShard.heartbeat.keepAlive) {
|
|
gateway.debug("GW HEARTBEATING_CLOSED", { shardId, shard: currentShard });
|
|
|
|
// STOP THE HEARTBEAT
|
|
return clearInterval(shard.heartbeat.intervalId);
|
|
}
|
|
|
|
if (!currentShard.heartbeat.acknowledged) {
|
|
gateway.closeWS(currentShard.ws, 3066, "Did not receive an ACK in time.");
|
|
return await gateway.identify(gateway, shardId, gateway.maxShards);
|
|
}
|
|
|
|
if (currentShard.ws.readyState !== WebSocket.OPEN) return;
|
|
|
|
currentShard.heartbeat.acknowledged = false;
|
|
|
|
currentShard.ws.send(
|
|
JSON.stringify({
|
|
op: GatewayOpcodes.Heartbeat,
|
|
d: currentShard.previousSequenceNumber,
|
|
}),
|
|
);
|
|
}, shard.heartbeat.interval);
|
|
}
|