From a9ddbd1e3c09b537f734a6903083f0de9d9feab3 Mon Sep 17 00:00:00 2001 From: Owen Schwartz Date: Sun, 10 Nov 2024 21:06:36 -0500 Subject: [PATCH] Add message handler seperate file --- server/routers/messageHandlers.ts | 6 ++++++ server/routers/ws.ts | 8 ++------ 2 files changed, 8 insertions(+), 6 deletions(-) create mode 100644 server/routers/messageHandlers.ts diff --git a/server/routers/messageHandlers.ts b/server/routers/messageHandlers.ts new file mode 100644 index 00000000..5555f3fe --- /dev/null +++ b/server/routers/messageHandlers.ts @@ -0,0 +1,6 @@ +import { handleNewtMessage } from "./newt"; +import { MessageHandler } from "./ws"; + +export const messageHandlers: Record = { + "newt": handleNewtMessage, +}; \ No newline at end of file diff --git a/server/routers/ws.ts b/server/routers/ws.ts index 965c3886..6ed2099b 100644 --- a/server/routers/ws.ts +++ b/server/routers/ws.ts @@ -7,7 +7,7 @@ import { Newt, newts, NewtSession } from "@server/db/schema"; import { eq } from "drizzle-orm"; import db from "@server/db"; import { validateNewtSessionToken } from "@server/auth/newt"; -import { handleNewtMessage } from "./newt"; +import { messageHandlers } from "./messageHandlers"; // Custom interfaces interface WebSocketRequest extends IncomingMessage { @@ -47,10 +47,6 @@ interface HandlerContext { export type MessageHandler = (context: HandlerContext) => Promise; -const messageHandlers: Record = { - "newt": handleNewtMessage, -}; - const router: Router = Router(); const wss: WebSocketServer = new WebSocketServer({ noServer: true }); @@ -202,7 +198,7 @@ wss.on("connection", (ws: AuthenticatedWebSocket, request: WebSocketRequest) => ws.on("message", async (data) => { try { const message: WSMessage = JSON.parse(data.toString()); - console.log(`Message received from Newt ID ${newtId}:`, message); + // console.log(`Message received from Newt ID ${newtId}:`, message); // Validate message format if (!message.type || typeof message.type !== "string") {