diff options
author | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2022-08-30 15:05:23 +1000 |
---|---|---|
committer | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2022-08-30 15:08:18 +1000 |
commit | 16315a3170ec018a834e68360e06b506415446d2 (patch) | |
tree | 90cfe456040fce35b904e88462886e3c73a2f3f2 /src/gateway/util/Send.ts | |
parent | Start listening after database and config has been loaded (diff) | |
parent | Oop, deprecated typeorm call (diff) | |
download | server-16315a3170ec018a834e68360e06b506415446d2.tar.xz |
Merge branch 'staging' into dev/Maddy/fix/listeningAfterDb
Diffstat (limited to 'src/gateway/util/Send.ts')
-rw-r--r-- | src/gateway/util/Send.ts | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/gateway/util/Send.ts b/src/gateway/util/Send.ts new file mode 100644 index 00000000..7826dd40 --- /dev/null +++ b/src/gateway/util/Send.ts @@ -0,0 +1,32 @@ +let erlpack: any; +try { + erlpack = require("@yukikaze-bot/erlpack"); +} catch (error) { + console.log("Missing @yukikaze-bot/erlpack, electron-based desktop clients designed for discord.com will not be able to connect!"); +} +import { Payload, WebSocket } from "@fosscord/gateway"; + +export async function Send(socket: WebSocket, data: Payload) { + if (process.env.WS_VERBOSE) console.log(`[Websocket] Outgoing message: ${JSON.stringify(data)}`); + 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); + else return; + // TODO: compression + if (socket.deflate) { + socket.deflate.write(buffer); + socket.deflate.flush(); + return; + } + + return new Promise((res, rej) => { + if (socket.readyState !== 1) { + return rej("socket not open"); + } + socket.send(buffer, (err: any) => { + if (err) return rej(err); + return res(null); + }); + }); +} |