Twan07 commited on
Commit
9e9075b
·
verified ·
1 Parent(s): 7fc3c0c

Create src/ws/adminWs.js

Browse files
Files changed (1) hide show
  1. src/ws/adminWs.js +24 -0
src/ws/adminWs.js ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { WebSocketServer } from "ws";
2
+ import jwt from "jsonwebtoken";
3
+
4
+ const clients = new Set();
5
+
6
+ export function initAdminWS(server) {
7
+ const wss = new WebSocketServer({ server, path: "/ws/admin" });
8
+
9
+ wss.on("connection", (ws, req) => {
10
+ const token = new URL(req.url, "http://x").searchParams.get("token");
11
+ try {
12
+ jwt.verify(token, process.env.JWT_SECRET);
13
+ clients.add(ws);
14
+ } catch {
15
+ ws.close();
16
+ }
17
+ ws.on("close", () => clients.delete(ws));
18
+ });
19
+ }
20
+
21
+ export function emitAdminAlert(data) {
22
+ const msg = JSON.stringify(data);
23
+ clients.forEach((ws) => ws.send(msg));
24
+ }