From b0ccb6857edfcae0f3bb6bc4af99fad6e46f8169 Mon Sep 17 00:00:00 2001 From: Hri7566 Date: Thu, 7 Sep 2023 17:24:39 -0400 Subject: [PATCH] Implement uWebSockets --- src/http/fastify.ts | 5 ----- src/http/socket.ts | 0 src/index.ts | 11 +++++++---- src/util/Logger.ts | 39 +++++++++++++++++++++++++++++++++++++++ src/ws/server.ts | 18 ++++++++++++++++++ 5 files changed, 64 insertions(+), 9 deletions(-) delete mode 100644 src/http/fastify.ts delete mode 100644 src/http/socket.ts create mode 100755 src/util/Logger.ts create mode 100644 src/ws/server.ts diff --git a/src/http/fastify.ts b/src/http/fastify.ts deleted file mode 100644 index d634148..0000000 --- a/src/http/fastify.ts +++ /dev/null @@ -1,5 +0,0 @@ -import fastify from "fastify"; - -export const app = fastify({ - -}); diff --git a/src/http/socket.ts b/src/http/socket.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/index.ts b/src/index.ts index 4d52db6..5e00ad7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,9 @@ -import { app } from "http/fastify"; -import env from "util/env"; +import env from "./util/env"; +import { app } from "./ws/server"; +import { Logger } from "./util/Logger"; -app.listen({ - port: env.PORT +const logger = new Logger("Main"); + +app.listen("0.0.0.0", env.PORT, () => { + logger.info("Listening on :" + env.PORT); }); diff --git a/src/util/Logger.ts b/src/util/Logger.ts new file mode 100755 index 0000000..15f3284 --- /dev/null +++ b/src/util/Logger.ts @@ -0,0 +1,39 @@ +export class Logger { + private static log(method: string, ...args: any[]) { + (console as unknown as Record any>)[ + method + ](this.getHHMMSS(), ...args); + } + + public static getHHMMSS() { + const ms = Date.now(); + + const s = ms / 1000; + const m = s / 60; + const h = m / 60; + + const ss = Math.floor(s) % 60; + const mm = Math.floor(m) % 60; + const hh = Math.floor(h) % 24; + + return `${hh}:${mm}:${ss}`; + } + + constructor(public id: string) {} + + public info(...args: any[]) { + Logger.log("log", `[${this.id}]`, `[\x1b[34mINFO\x1b[0m]`, ...args); + } + + public error(...args: any[]) { + Logger.log("error", `[${this.id}]`, `[\x1b[31mERROR\x1b[0m]`, ...args); + } + + public warn(...args: any[]) { + Logger.log("warn", `[${this.id}]`, `[\x1b[33mWARN\x1b[0m]`, ...args); + } + + public debug(...args: any[]) { + Logger.log("debug", `[${this.id}]`, `[\x1b[32mDEBUG\x1b[0m]`, ...args); + } +} diff --git a/src/ws/server.ts b/src/ws/server.ts new file mode 100644 index 0000000..9895057 --- /dev/null +++ b/src/ws/server.ts @@ -0,0 +1,18 @@ +import { App, DEDICATED_COMPRESSOR_8KB } from "uWebSockets.js"; +import { Logger } from "../util/Logger"; + +const logger = new Logger("WebSocket Server"); + +export const app = App() + .get("/", (res, req) => {}) + .ws("/*", { + idleTimeout: 30, + maxBackpressure: 1024, + maxPayloadLength: 8192, + compression: DEDICATED_COMPRESSOR_8KB, + + message: (ws, message, isBinary) => { + const msg = String(message); + logger.debug(msg); + } + });