Estrae la gestione dei messaggi WebSocket in un modulo dedicato. Rende server.js più snello e focalizzato su bootstrap HTTP/WS. Introduce utility per stampa URL di accesso e discovery IP di rete. Mantiene la logica di stato partita condivisa in gameState.js.
35 lines
1.0 KiB
JavaScript
35 lines
1.0 KiB
JavaScript
import { createServer } from 'http'
|
|
import express from 'express'
|
|
import { WebSocketServer } from 'ws'
|
|
import { fileURLToPath } from 'url'
|
|
import { dirname, join } from 'path'
|
|
import { setupWebSocketHandler } from './src/websocket-handler.js'
|
|
import { printServerInfo } from './src/server-utils.js'
|
|
|
|
const __filename = fileURLToPath(import.meta.url)
|
|
const __dirname = dirname(__filename)
|
|
|
|
// --- Configurazione del server ---
|
|
|
|
const app = express()
|
|
const PORT = process.env.PORT || 3000
|
|
|
|
// Espone i file generati dalla build di Vite.
|
|
app.use(express.static(join(__dirname, 'dist')))
|
|
|
|
// Fallback per SPA: restituisce `index.html` per tutte le route che non puntano a file statici.
|
|
app.get('/{*splat}', (_req, res) => {
|
|
res.sendFile(join(__dirname, 'dist', 'index.html'))
|
|
})
|
|
|
|
const server = createServer(app)
|
|
|
|
// Inizializza il server WebSocket con la logica di gioco.
|
|
const wss = new WebSocketServer({ server })
|
|
setupWebSocketHandler(wss)
|
|
|
|
// Avvia il server HTTP.
|
|
server.listen(PORT, '0.0.0.0', () => {
|
|
printServerInfo(PORT)
|
|
})
|