summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/Constants.ts1
-rw-r--r--src/util/Send.ts21
-rw-r--r--src/util/WebSocket.ts13
-rw-r--r--src/util/setHeartbeat.ts10
4 files changed, 45 insertions, 0 deletions
diff --git a/src/util/Constants.ts b/src/util/Constants.ts

index cdc0d07d..e8e91d69 100644 --- a/src/util/Constants.ts +++ b/src/util/Constants.ts
@@ -4,6 +4,7 @@ export enum OPCODES { Identify, Presence_Update, Voice_State_Update, + Dummy_Value, // ? What is opcode 5? Resume, Reconnect, Request_Guild_Members, diff --git a/src/util/Send.ts b/src/util/Send.ts new file mode 100644
index 00000000..d38865b3 --- /dev/null +++ b/src/util/Send.ts
@@ -0,0 +1,21 @@ +import erlpack from "erlpack"; +import { promisify } from "util"; +import { Payload } from "../util/Constants"; + +import WebSocket from "./WebSocket"; + +export async function Send(socket: WebSocket, data: Payload) { + let buffer: Buffer | string; + if (socket.encoding === "etf") buffer = erlpack.pack(data); + // TODO: encode circular object + else if (socket.encoding === "json") buffer = JSON.stringify(data); + + // TODO: compression + + return new Promise((res, rej) => { + socket.send(buffer, (err) => { + if (err) return rej(err); + return res(null); + }); + }); +} diff --git a/src/util/WebSocket.ts b/src/util/WebSocket.ts new file mode 100644
index 00000000..41ce8851 --- /dev/null +++ b/src/util/WebSocket.ts
@@ -0,0 +1,13 @@ +import WS, { Server, Data } from "ws"; + +interface WebSocket extends WS { + version: number; + userid: bigint; + encoding: "etf" | "json"; + compress?: "zlib-stream"; + heartbeatTimeout: NodeJS.Timeout; + readyTimeout: NodeJS.Timeout; +} + +export default WebSocket; +export { Server, Data }; diff --git a/src/util/setHeartbeat.ts b/src/util/setHeartbeat.ts new file mode 100644
index 00000000..1fe657a8 --- /dev/null +++ b/src/util/setHeartbeat.ts
@@ -0,0 +1,10 @@ +import WebSocket from "./WebSocket"; + +// TODO: make heartbeat timeout configurable +export function setHeartbeat(socket: WebSocket) { + if (socket.heartbeatTimeout) clearTimeout(socket.heartbeatTimeout); + + socket.heartbeatTimeout = setTimeout(() => { + return socket.close(4009); + }, 1000 * 30); +}