Integrates static file serving for HTML, CSS, and JS, and sets up a WebSocket server for real-time communication. Replit-Commit-Author: Agent Replit-Commit-Session-Id: f3ac8eb3-f610-4678-ab6e-ebf900098be4 Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Event-Id: 182c9671-a459-4b94-a7eb-1c7a0cefe768 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/d4b7863b-f7b2-425c-a9b5-ad7bd1885e9d/f3ac8eb3-f610-4678-ab6e-ebf900098be4/N1qS6qo Replit-Helium-Checkpoint-Created: true
47 lines
1.4 KiB
TypeScript
47 lines
1.4 KiB
TypeScript
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<Server> {
|
|
// 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;
|
|
}
|