diff options
Diffstat (limited to 'gateway/src/util/Send.ts')
-rw-r--r-- | gateway/src/util/Send.ts | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/gateway/src/util/Send.ts b/gateway/src/util/Send.ts new file mode 100644 index 00000000..be25ac4f --- /dev/null +++ b/gateway/src/util/Send.ts @@ -0,0 +1,28 @@ +var erlpack: any; +try { + erlpack = require("erlpack"); +} catch (error) {} +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); + else return; + // TODO: compression + if (socket.deflate) { + socket.deflate.write(buffer); + socket.deflate.flush(); + return; + } + + return new Promise((res, rej) => { + socket.send(buffer, (err) => { + if (err) return rej(err); + return res(null); + }); + }); +} |