version 0.0.0.3
This commit is contained in:
+7
-14
@@ -4,7 +4,6 @@ const { WebSocketServer } = require("ws");
|
||||
|
||||
// ── Konfiguration aus Umgebungsvariablen ────────────────────────────
|
||||
const PORT = parseInt(process.env.PORT || "3000", 10);
|
||||
const TOKEN_EXPIRY = parseInt(process.env.TOKEN_EXPIRY || "3600", 10) * 1000; // in ms
|
||||
const MAX_SESSIONS = parseInt(process.env.MAX_SESSIONS || "10", 10);
|
||||
|
||||
// Erlaubte Nachrichtentypen — alles andere wird verworfen
|
||||
@@ -12,7 +11,7 @@ const ALLOWED_TYPES = new Set([
|
||||
"chat", "audio", "file", "location", "mode", "log", "event",
|
||||
]);
|
||||
|
||||
// Token-Raum: token -> { clients: Set<ws>, createdAt: number }
|
||||
// Token-Raum: token -> { clients: Set<ws> }
|
||||
const rooms = new Map();
|
||||
|
||||
// ── Hilfsfunktionen ─────────────────────────────────────────────────
|
||||
@@ -25,22 +24,17 @@ function log(msg) {
|
||||
console.log(`[${timestamp()}] ${msg}`);
|
||||
}
|
||||
|
||||
// Abgelaufene und leere Räume aufräumen
|
||||
// Leere Räume und tote Clients aufräumen
|
||||
function cleanupRooms() {
|
||||
const now = Date.now();
|
||||
for (const [token, room] of rooms) {
|
||||
// Tote Clients entfernen
|
||||
for (const client of room.clients) {
|
||||
if (client.readyState > 1) room.clients.delete(client);
|
||||
}
|
||||
// Raum löschen wenn leer oder abgelaufen
|
||||
if (room.clients.size === 0 || now - room.createdAt > TOKEN_EXPIRY) {
|
||||
// Verbleibende Clients sauber trennen
|
||||
for (const client of room.clients) {
|
||||
client.close(4001, "Token abgelaufen");
|
||||
}
|
||||
// Raum löschen wenn leer
|
||||
if (room.clients.size === 0) {
|
||||
rooms.delete(token);
|
||||
log(`Raum entfernt: ${token.slice(0, 8)}... (${room.clients.size} Clients)`);
|
||||
log(`Leerer Raum entfernt: ${token.slice(0, 8)}...`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,8 +44,7 @@ function cleanupRooms() {
|
||||
const wss = new WebSocketServer({ port: PORT });
|
||||
|
||||
wss.on("listening", () => {
|
||||
log(`RVS läuft auf Port ${PORT}`);
|
||||
log(`Token-Ablauf: ${TOKEN_EXPIRY / 1000}s | Max Sessions: ${MAX_SESSIONS}`);
|
||||
log(`RVS läuft auf Port ${PORT} | Max Sessions: ${MAX_SESSIONS}`);
|
||||
});
|
||||
|
||||
wss.on("connection", (ws, req) => {
|
||||
@@ -89,7 +82,7 @@ function registerClient(ws, token) {
|
||||
|
||||
// Raum erstellen oder betreten
|
||||
if (!rooms.has(token)) {
|
||||
rooms.set(token, { clients: new Set(), createdAt: Date.now() });
|
||||
rooms.set(token, { clients: new Set() });
|
||||
log(`Neuer Raum: ${token.slice(0, 8)}...`);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user