Working request id response

This commit is contained in:
2026-02-16 01:01:29 +01:00
parent b2ba4fbcc8
commit 5a0c83c4c1
2 changed files with 31 additions and 7 deletions
+28 -5
View File
@@ -15,7 +15,10 @@ export function GET(req: NextRequest) {
export function UPGRADE(client: WebSocket, server: WebSocketServer) { export function UPGRADE(client: WebSocket, server: WebSocketServer) {
client.once("message", async (raw) => { client.once("message", async (raw) => {
const { cookies }: { cookies: Record<string, string> } = JSON.parse( const {
cookies,
requestId,
}: { cookies: Record<string, string>; requestId: string } = JSON.parse(
raw.toString(), raw.toString(),
); );
@@ -33,11 +36,13 @@ export function UPGRADE(client: WebSocket, server: WebSocketServer) {
return; return;
} }
client.send(JSON.stringify({ ok: true })); client.send(JSON.stringify({ requestId, ok: true }));
console.log(`User ${session[1].username} connected to live run`);
client.on("message", async (raw) => { client.on("message", async (raw) => {
try { try {
const msg: ClientMessage = JSON.parse(raw.toString()); const { requestId, ...msg }: ClientMessage = JSON.parse(raw.toString());
switch (msg.type) { switch (msg.type) {
/* ---------------- CREATE ---------------- */ /* ---------------- CREATE ---------------- */
@@ -47,6 +52,7 @@ export function UPGRADE(client: WebSocket, server: WebSocketServer) {
if (!problem || !category) { if (!problem || !category) {
client.send( client.send(
JSON.stringify({ JSON.stringify({
requestId,
ok: false, ok: false,
error: "problem and category required", error: "problem and category required",
}), }),
@@ -68,6 +74,7 @@ export function UPGRADE(client: WebSocket, server: WebSocketServer) {
client.send( client.send(
JSON.stringify({ JSON.stringify({
requestId,
ok: true, ok: true,
type: "create", type: "create",
data: { runId }, data: { runId },
@@ -88,6 +95,7 @@ export function UPGRADE(client: WebSocket, server: WebSocketServer) {
) { ) {
client.send( client.send(
JSON.stringify({ JSON.stringify({
requestId,
ok: false, ok: false,
error: "body requires runId, moves, file and language", error: "body requires runId, moves, file and language",
}), }),
@@ -100,6 +108,7 @@ export function UPGRADE(client: WebSocket, server: WebSocketServer) {
if (!liveRun) { if (!liveRun) {
client.send( client.send(
JSON.stringify({ JSON.stringify({
requestId,
ok: false, ok: false,
error: "Run is not live or does not exist", error: "Run is not live or does not exist",
}), }),
@@ -116,6 +125,7 @@ export function UPGRADE(client: WebSocket, server: WebSocketServer) {
client.send( client.send(
JSON.stringify({ JSON.stringify({
requestId,
ok: true, ok: true,
type: "move", type: "move",
data: run, data: run,
@@ -131,7 +141,11 @@ export function UPGRADE(client: WebSocket, server: WebSocketServer) {
if (!runId) { if (!runId) {
client.send( client.send(
JSON.stringify({ ok: false, error: "runId required" }), JSON.stringify({
requestId,
ok: false,
error: "runId required",
}),
); );
return; return;
} }
@@ -141,6 +155,7 @@ export function UPGRADE(client: WebSocket, server: WebSocketServer) {
if (!liveRun) { if (!liveRun) {
client.send( client.send(
JSON.stringify({ JSON.stringify({
requestId,
ok: false, ok: false,
error: "Run is not live or does not exist", error: "Run is not live or does not exist",
}), }),
@@ -152,6 +167,7 @@ export function UPGRADE(client: WebSocket, server: WebSocketServer) {
client.send( client.send(
JSON.stringify({ JSON.stringify({
requestId,
ok: true, ok: true,
type: "submit", type: "submit",
data: run, data: run,
@@ -166,7 +182,11 @@ export function UPGRADE(client: WebSocket, server: WebSocketServer) {
if (!runId) { if (!runId) {
client.send( client.send(
JSON.stringify({ ok: false, error: "runId required" }), JSON.stringify({
requestId,
ok: false,
error: "runId required",
}),
); );
return; return;
} }
@@ -175,6 +195,7 @@ export function UPGRADE(client: WebSocket, server: WebSocketServer) {
client.send( client.send(
JSON.stringify({ JSON.stringify({
requestId,
ok: true, ok: true,
type: "delete", type: "delete",
data: run, data: run,
@@ -186,6 +207,7 @@ export function UPGRADE(client: WebSocket, server: WebSocketServer) {
default: default:
client.send( client.send(
JSON.stringify({ JSON.stringify({
requestId,
ok: false, ok: false,
error: "Unknown message type", error: "Unknown message type",
}), }),
@@ -195,6 +217,7 @@ export function UPGRADE(client: WebSocket, server: WebSocketServer) {
console.error(err); console.error(err);
client.send( client.send(
JSON.stringify({ JSON.stringify({
requestId,
ok: false, ok: false,
error: "Malformed message", error: "Malformed message",
}), }),
+3 -2
View File
@@ -1,6 +1,6 @@
import { LiveRunMove } from "@/modules/live-run/types"; import { LiveRunMove } from "@/modules/live-run/types";
export type ClientMessage = export type ClientMessage = { requestId: string } & (
| { | {
type: "create"; type: "create";
problem: string; problem: string;
@@ -20,7 +20,8 @@ export type ClientMessage =
| { | {
type: "delete"; type: "delete";
runId: string; runId: string;
}; }
);
export type ServerMessage = export type ServerMessage =
| { ok: true; type: string; data?: any } | { ok: true; type: string; data?: any }