forked from Hri7566/mpp-server-dev2
Implement uWebSockets
This commit is contained in:
parent
d43f6cba2d
commit
b0ccb6857e
|
@ -1,5 +0,0 @@
|
|||
import fastify from "fastify";
|
||||
|
||||
export const app = fastify({
|
||||
|
||||
});
|
11
src/index.ts
11
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);
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
});
|
Loading…
Reference in New Issue