update sequence number and keep gateway alive

This commit is contained in:
Skillz
2020-02-09 14:20:39 -05:00
parent 41e26e4a6b
commit a2b2c79605
2 changed files with 12 additions and 6 deletions

View File

@@ -13,7 +13,7 @@ import {
// import { BufReader } from "https://deno.land/std/io/bufio.ts"
// import { TextProtoReader } from "https://deno.land/std/textproto/mod.ts"
import { blue, green, red, yellow } from "https://deno.land/std/fmt/colors.ts"
import { keepDiscordWebsocketAlive } from "./websocket.ts";
import { keepDiscordWebsocketAlive, updatePreviousSequenceNumber } from "./websocket.ts";
class Client {
/** The bot's token. This should never be used by end users. It is meant to be used internally to make requests to the Discord API. */
@@ -65,6 +65,10 @@ class Client {
switch (data.op) {
case 10: // Initial Heartbeat
keepDiscordWebsocketAlive(socket, (data.d as DiscordHeartbeatPayload).heartbeat_interval, data.s)
break
case 11:
updatePreviousSequenceNumber(data.s)
break
}
}

View File

@@ -1,15 +1,17 @@
import { WebSocket } from "https://deno.land/std/ws/mod.ts";
export const keepDiscordWebsocketAlive = (socket: WebSocket, millesecondsInterval: number, payload: number | null = null) => {
let previousSequenceNumber = payload
let previousSequenceNumber: number | null = null
export const keepDiscordWebsocketAlive = (socket: WebSocket, millesecondsInterval: number, payload: number | null = null) => {
setInterval(() => {
const response = await socket.send(JSON.stringify({
socket.send(JSON.stringify({
op: 1,
d: previousSequenceNumber
}))
console.log(response)
}, millesecondsInterval)
}
export const updatePreviousSequenceNumber = (sequence: number | null = null) => {
previousSequenceNumber = sequence
}