Add message handler seperate file

This commit is contained in:
Owen Schwartz 2024-11-10 21:06:36 -05:00
parent b8a0d08005
commit a9ddbd1e3c
2 changed files with 8 additions and 6 deletions

View file

@ -0,0 +1,6 @@
import { handleNewtMessage } from "./newt";
import { MessageHandler } from "./ws";
export const messageHandlers: Record<string, MessageHandler> = {
"newt": handleNewtMessage,
};

View file

@ -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<HandlerResponse | void>;
const messageHandlers: Record<string, MessageHandler> = {
"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") {