mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-30 14:39:29 +02:00
Add message handler seperate file
This commit is contained in:
parent
b8a0d08005
commit
a9ddbd1e3c
2 changed files with 8 additions and 6 deletions
6
server/routers/messageHandlers.ts
Normal file
6
server/routers/messageHandlers.ts
Normal file
|
@ -0,0 +1,6 @@
|
|||
import { handleNewtMessage } from "./newt";
|
||||
import { MessageHandler } from "./ws";
|
||||
|
||||
export const messageHandlers: Record<string, MessageHandler> = {
|
||||
"newt": handleNewtMessage,
|
||||
};
|
|
@ -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") {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue