JWT, server modüler hale getirildi, Torrent durumu kalıcı hale getirildi.

This commit is contained in:
2025-11-29 01:42:43 +03:00
parent f4c9d4ca41
commit 08b25b418e
13 changed files with 759 additions and 285 deletions

View File

@@ -0,0 +1,35 @@
import { WebSocketServer } from "ws";
import url from "url";
function parseTokenFromRequest(req) {
try {
const parsed = url.parse(req.url, true);
return parsed.query?.token || null;
} catch (err) {
return null;
}
}
export function createWebsocketServer(server, { verifyToken, onMessage }) {
const wss = new WebSocketServer({ server });
wss.on("connection", (ws, req) => {
const token = parseTokenFromRequest(req);
const decoded = token && verifyToken ? verifyToken(token, "access") : null;
if (!decoded) {
ws.close();
return;
}
ws.user = decoded;
ws.on("message", (msg) => onMessage && onMessage(msg, ws));
ws.on("error", (err) => console.warn("🔌 WebSocket error:", err.message));
});
return wss;
}
export function broadcastJson(wss, payload) {
if (!wss) return;
const data = JSON.stringify(payload);
wss.clients.forEach((c) => c.readyState === 1 && c.send(data));
}