import type { Express } from "express"; import { createServer, type Server } from "http"; import { WebSocketServer } from "ws"; import fs from "fs"; import path from "path"; import { handleMessage, handleClose } from "./buzzer-ws"; export async function registerRoutes( httpServer: Server, app: Express ): Promise { // Serve buzzer static files const publicDir = path.resolve(process.cwd(), "src/public"); app.get("/", (_req, res) => { res.setHeader("Content-Type", "text/html; charset=utf-8"); res.send(fs.readFileSync(path.join(publicDir, "index.html"), "utf-8")); }); app.get("/styles.css", (_req, res) => { res.setHeader("Content-Type", "text/css"); res.send(fs.readFileSync(path.join(publicDir, "styles.css"), "utf-8")); }); app.get("/script.js", (_req, res) => { res.setHeader("Content-Type", "text/javascript"); res.send(fs.readFileSync(path.join(publicDir, "script.js"), "utf-8")); }); // WebSocket server for buzzer const wss = new WebSocketServer({ server: httpServer, path: "/ws" }); wss.on("connection", (ws) => { ws.on("message", (data) => { if (typeof data === "string") { handleMessage(ws, data); } else { handleMessage(ws, data.toString()); } }); ws.on("close", () => handleClose(ws)); ws.on("error", () => { try { ws.close(); } catch {} }); }); return httpServer; }