Implement uWebSockets

This commit is contained in:
Hri7566 2023-09-07 17:24:39 -04:00
parent d43f6cba2d
commit b0ccb6857e
5 changed files with 64 additions and 9 deletions

View File

@ -1,5 +0,0 @@
import fastify from "fastify";
export const app = fastify({
});

View File

View File

@ -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({ const logger = new Logger("Main");
port: env.PORT
app.listen("0.0.0.0", env.PORT, () => {
logger.info("Listening on :" + env.PORT);
}); });

39
src/util/Logger.ts Executable file
View File

@ -0,0 +1,39 @@
export class Logger {
private static log(method: string, ...args: any[]) {
(console as unknown as Record<string, (..._args: any[]) => 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);
}
}

18
src/ws/server.ts Normal file
View File

@ -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);
}
});