Update buzzer app to serve static files and handle WebSocket connections
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
This commit is contained in:
@@ -1,16 +1,46 @@
|
||||
import type { Express } from "express";
|
||||
import { createServer, type Server } from "http";
|
||||
import { storage } from "./storage";
|
||||
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> {
|
||||
// put application routes here
|
||||
// prefix all routes with /api
|
||||
// Serve buzzer static files
|
||||
const publicDir = path.resolve(process.cwd(), "src/public");
|
||||
|
||||
// use storage to perform CRUD operations on the storage interface
|
||||
// e.g. storage.insertUser(user) or storage.getUserByUsername(username)
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user