| import { WebSocketServer } from "ws"; | |
| import jwt from "jsonwebtoken"; | |
| const clients = new Set(); | |
| export function initAdminWS(server) { | |
| const wss = new WebSocketServer({ server, path: "/ws/admin" }); | |
| wss.on("connection", (ws, req) => { | |
| const token = new URL(req.url, "http://x").searchParams.get("token"); | |
| try { | |
| jwt.verify(token, process.env.JWT_SECRET); | |
| clients.add(ws); | |
| } catch { | |
| ws.close(); | |
| } | |
| ws.on("close", () => clients.delete(ws)); | |
| }); | |
| } | |
| export function emitAdminAlert(data) { | |
| const msg = JSON.stringify(data); | |
| clients.forEach((ws) => ws.send(msg)); | |
| } | |