Refactor server.js to simplify and improve WebSocket handling
This commit is contained in:
@@ -1,73 +1,24 @@
|
|||||||
// server/server.js
|
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const http = require('http');
|
const http = require('http');
|
||||||
const path = require('path');
|
|
||||||
const WebSocket = require('ws');
|
const WebSocket = require('ws');
|
||||||
|
const path = require('path');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
// --- Grundlegende Config ---
|
// --- App & HTTP-Server
|
||||||
const PORT = 3000;
|
|
||||||
const PUBLIC_DIR = path.join(__dirname, 'public');
|
|
||||||
const SPIRITS_DIR = path.join(__dirname, 'spirits');
|
|
||||||
|
|
||||||
// --- Webserver (Express) ---
|
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(express.static(PUBLIC_DIR));
|
|
||||||
|
|
||||||
// API: Liste aller Spirits (mit Name, Beschreibung, Model-URL etc.)
|
|
||||||
app.get('/api/spirits', (req, res) => {
|
|
||||||
// z.B. spirit_list.json mit Array aus [{name,desc,modelUrl,...}]
|
|
||||||
const spiritsMeta = fs.readFileSync(path.join(SPIRITS_DIR, 'spirit_list.json'), 'utf-8');
|
|
||||||
res.json(JSON.parse(spiritsMeta));
|
|
||||||
});
|
|
||||||
|
|
||||||
// (Optional) Models/Bilder auch direkt serven:
|
|
||||||
app.use('/models', express.static(path.join(SPIRITS_DIR, 'models')));
|
|
||||||
app.use('/images', express.static(path.join(SPIRITS_DIR, 'images')));
|
|
||||||
|
|
||||||
// --- HTTP Server + WebSocket ---
|
|
||||||
const server = http.createServer(app);
|
const server = http.createServer(app);
|
||||||
const wss = new WebSocket.Server({ server });
|
const wss = new WebSocket.Server({ server });
|
||||||
|
|
||||||
let clients = [];
|
// --- Static files aus /public
|
||||||
let currentSpirit = null;
|
app.use(express.static(path.join(__dirname, 'public')));
|
||||||
let nextSpiritTimeout = null;
|
|
||||||
|
|
||||||
wss.on('connection', (ws) => {
|
// --- WebSocket: simple Broadcast Demo
|
||||||
clients.push(ws);
|
wss.on('connection', ws => {
|
||||||
console.log('Client connected:', clients.length, 'total');
|
ws.send(JSON.stringify({ type: 'spirit', data: { name: 'Demo', modelUrl: '/assets/models/spirits/Yuki_Onna.glb', desc: 'Testdesc.' } }));
|
||||||
// Beim Connect sofort aktuellen Spirit senden:
|
|
||||||
if (currentSpirit) ws.send(JSON.stringify({ type: 'spirit', data: currentSpirit }));
|
|
||||||
|
|
||||||
ws.on('close', () => {
|
|
||||||
clients = clients.filter(c => c !== ws);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Helper: Broadcast an alle
|
// --- Server starten
|
||||||
function broadcastSpirit(spiritObj) {
|
const PORT = process.env.PORT || 3000;
|
||||||
const msg = JSON.stringify({ type: 'spirit', data: spiritObj });
|
|
||||||
clients.forEach(ws => { try { ws.send(msg); } catch {} });
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- Spirit-Steuerung ---
|
|
||||||
function chooseRandomSpirit() {
|
|
||||||
const spirits = JSON.parse(fs.readFileSync(path.join(SPIRITS_DIR, 'spirit_list.json'), 'utf-8'));
|
|
||||||
return spirits[Math.floor(Math.random()*spirits.length)];
|
|
||||||
}
|
|
||||||
|
|
||||||
function scheduleNextSpirit() {
|
|
||||||
const spirit = chooseRandomSpirit();
|
|
||||||
currentSpirit = spirit;
|
|
||||||
broadcastSpirit(spirit);
|
|
||||||
// Alle 10 Sekunden neues Spirit:
|
|
||||||
nextSpiritTimeout = setTimeout(scheduleNextSpirit, 10000);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Starte Zyklus
|
|
||||||
scheduleNextSpirit();
|
|
||||||
|
|
||||||
// --- Server Start ---
|
|
||||||
server.listen(PORT, () => {
|
server.listen(PORT, () => {
|
||||||
console.log(`Server running at http://localhost:${PORT}`);
|
console.log('Server läuft auf http://localhost:' + PORT);
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user