Well, sending messages took longer than it should have

This commit is contained in:
metacryst
2025-11-25 23:38:59 -06:00
parent dc9b106439
commit 9e87364147
25 changed files with 550 additions and 189 deletions

View File

@@ -2,6 +2,7 @@ const { WebSocket, WebSocketServer } = require('ws');
const { z } = require("zod")
const jwt = require('jsonwebtoken');
import ForumHandler from "./handlers/ForumHandler.js"
import MessagesHandler from "./handlers/MessagesHandler.js"
export default class Socket {
wss;
@@ -12,8 +13,18 @@ export default class Socket {
msg: z.union([
z.object({}).passthrough(), // allows any object
z.array(z.any()) // allows any array
])
}).strict()
]).optional()
})
.superRefine((data, ctx) => {
if (data.operation !== "GET" && data.msg === undefined) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ["msg"],
message: "msg is required when operation is not GET"
})
}
})
.strict()
constructor(server) {
this.wss = new WebSocketServer({ server });
@@ -54,7 +65,7 @@ export default class Socket {
try {
const text = msg.toString();
const req = JSON.parse(text);
if(!this.messageSchema.safeParse(req).success) throw new Error("Socket.handleMessage: Incorrectly formatted incoming ws message!")
if(!this.messageSchema.safeParse(req).success) throw new Error("Socket.handleMessage: Incoming ws message has incorrect format!")
let responseData;
switch (req.app) {
@@ -62,6 +73,10 @@ export default class Socket {
responseData = ForumHandler.handle(req.operation, req.msg, ws)
break;
case "MESSAGES":
responseData = MessagesHandler.handle(req.operation, req.msg, ws)
break;
default:
console.error("unknown ws message")
}
@@ -71,7 +86,7 @@ export default class Socket {
}
response.msg = responseData
if(!this.messageSchema.safeParse(response).success) throw new Error("Socket.handleMessage: Incorrectly formatted outgoing ws message!")
if(!this.messageSchema.safeParse(response).success) throw new Error("Socket.handleMessage: Outgoing ws message has incorrect format!")
ws.send(JSON.stringify(response))
} catch (e) {