refactor(server): separa la logica WebSocket e centralizza le utility di avvio
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.
This commit is contained in:
45
src/server-utils.js
Normal file
45
src/server-utils.js
Normal file
@@ -0,0 +1,45 @@
|
||||
import { networkInterfaces } from 'os'
|
||||
|
||||
/**
|
||||
* Restituisce gli indirizzi IP di rete del sistema, escludendo loopback e bridge Docker.
|
||||
* @returns {string[]} Elenco degli indirizzi IP disponibili.
|
||||
*/
|
||||
export function getNetworkIPs() {
|
||||
const nets = networkInterfaces()
|
||||
const networkIPs = []
|
||||
|
||||
for (const name of Object.keys(nets)) {
|
||||
for (const net of nets[name]) {
|
||||
// Esclude loopback (127.0.0.1), indirizzi non IPv4 e bridge Docker (172.17.x.x, 172.18.x.x).
|
||||
if (net.family === 'IPv4' &&
|
||||
!net.internal &&
|
||||
!net.address.startsWith('172.17.') &&
|
||||
!net.address.startsWith('172.18.')) {
|
||||
networkIPs.push(net.address)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return networkIPs
|
||||
}
|
||||
|
||||
/**
|
||||
* Stampa il riepilogo di avvio del server con gli URL di accesso.
|
||||
* @param {number} port - Porta sulla quale il server e in ascolto.
|
||||
*/
|
||||
export function printServerInfo(port = 5173) {
|
||||
const networkIPs = getNetworkIPs()
|
||||
|
||||
console.log(`\nSegnapunti Server`)
|
||||
console.log(` Display: http://localhost:${port}/`)
|
||||
console.log(` Controller: http://localhost:${port}/controller`)
|
||||
|
||||
if (networkIPs.length > 0) {
|
||||
console.log(`\n Da dispositivi remoti:`)
|
||||
networkIPs.forEach(ip => {
|
||||
console.log(` http://${ip}:${port}/controller`)
|
||||
})
|
||||
}
|
||||
|
||||
console.log()
|
||||
}
|
||||
Reference in New Issue
Block a user