Refactor server logic to include timer control and client management
This commit is contained in:
@@ -1,36 +1,5 @@
|
||||
// server/server.js
|
||||
|
||||
const express = require('express');
|
||||
const http = require('http');
|
||||
const ws = require('ws');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
// --- Server Setup ---
|
||||
const app = express();
|
||||
const server = http.createServer(app);
|
||||
const wss = new ws.Server({ server });
|
||||
|
||||
// Statisches Hosting von /public
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
|
||||
// --- Spirits laden ---
|
||||
const SPIRITS_PATH = path.join(__dirname, '.', 'spirits', 'spirit_list.json');
|
||||
let spirits = [];
|
||||
try {
|
||||
spirits = JSON.parse(fs.readFileSync(SPIRITS_PATH, 'utf8'));
|
||||
if (!Array.isArray(spirits) || spirits.length === 0) throw 'Spirit-Liste leer oder ungültig!';
|
||||
} catch (e) {
|
||||
console.error('Fehler beim Laden der Spirits:', e);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// --- WebSocket Logik ---
|
||||
wss.on('connection', (socket) => {
|
||||
console.log('Neuer Client verbunden');
|
||||
});
|
||||
|
||||
// --- Spirit-Zeitsteuerung ---
|
||||
// --- WebSocket Logik mit Timer-Steuerung ---
|
||||
let spiritTimer = null;
|
||||
let currentSpiritIndex = Math.floor(Math.random() * spirits.length);
|
||||
|
||||
function pushSpiritToAllClients() {
|
||||
@@ -44,14 +13,56 @@ function pushSpiritToAllClients() {
|
||||
console.log(`Spirit "${spirit.Name}" wurde an alle Clients gepusht.`);
|
||||
}
|
||||
|
||||
// --- Timer: Alle 25 Sekunden neues Spirit ---
|
||||
setInterval(() => {
|
||||
currentSpiritIndex = Math.floor(Math.random() * spirits.length);
|
||||
pushSpiritToAllClients();
|
||||
}, 20000);
|
||||
// Timer starten, falls noch nicht läuft
|
||||
function startSpiritTimer() {
|
||||
if (!spiritTimer) {
|
||||
spiritTimer = setInterval(() => {
|
||||
currentSpiritIndex = Math.floor(Math.random() * spirits.length);
|
||||
pushSpiritToAllClients();
|
||||
}, 20000);
|
||||
console.log('Spirit-Timer gestartet');
|
||||
}
|
||||
}
|
||||
|
||||
// Beim Start: Ersten Spirit pushen
|
||||
setTimeout(pushSpiritToAllClients, 1500);
|
||||
// Timer stoppen
|
||||
function stopSpiritTimer() {
|
||||
if (spiritTimer) {
|
||||
clearInterval(spiritTimer);
|
||||
spiritTimer = null;
|
||||
console.log('Spirit-Timer gestoppt');
|
||||
}
|
||||
}
|
||||
|
||||
// Helper: Gibt es noch offene Clients?
|
||||
function hasOpenClients() {
|
||||
let found = false;
|
||||
wss.clients.forEach(client => {
|
||||
if (client.readyState === ws.OPEN) found = true;
|
||||
});
|
||||
return found;
|
||||
}
|
||||
|
||||
// --- WebSocket Logik ---
|
||||
wss.on('connection', (socket) => {
|
||||
console.log('Neuer Client verbunden');
|
||||
|
||||
// Sende sofort einen Spirit an den neuen Client
|
||||
socket.send(JSON.stringify({ type: 'spirit', data: spirits[currentSpiritIndex] }));
|
||||
|
||||
// Starte Timer falls das der erste Client ist
|
||||
if (wss.clients.size === 1) {
|
||||
startSpiritTimer();
|
||||
}
|
||||
|
||||
// Verbindung verloren: Timer ggf. stoppen
|
||||
socket.on('close', () => {
|
||||
setTimeout(() => {
|
||||
if (!hasOpenClients()) {
|
||||
stopSpiritTimer();
|
||||
}
|
||||
}, 100); // etwas warten, falls kurzzeitig mehrere Clients disconnecten/reconnecten
|
||||
});
|
||||
});
|
||||
|
||||
// --- Server Start ---
|
||||
const PORT = process.env.PORT || 3000;
|
||||
|
||||
Reference in New Issue
Block a user