Files
discordeno/module/gateway.ts
2020-05-06 19:02:43 -04:00

18 lines
879 B
TypeScript

import { WebSocket } from "https://deno.land/std@v1.0.0-rc1/ws/mod.ts"
import { GatewayOpcode } from "../types/discord.ts"
import { delay } from "https://deno.land/std@v1.0.0-rc1/util/async.ts"
// Discord requests null if no number has yet been sent by discord
export let previousSequenceNumber: number | null = null
// TODO: If a client does not receive a heartbeat ack between its attempts at sending heartbeats, it should immediately terminate the connection with a non-1000 close code, reconnect, and attempt to resume.
export const sendConstantHeartbeats = async (socket: WebSocket, interval: number) => {
await delay(interval)
socket.send(JSON.stringify({ op: GatewayOpcode.Heartbeat, d: previousSequenceNumber }))
sendConstantHeartbeats(socket, interval)
}
export const updatePreviousSequenceNumber = (sequence: number) => {
previousSequenceNumber = sequence
}