chore(dev): aggiorna workflow locale e configurazione Vite
Introduce script di sviluppo concorrenti (frontend + server) con concurrently. Aggiorna dipendenze lockfile e rimuove dipendenze non più necessarie. Aggiunge configurazione server/proxy Vite e include plugin WebSocket dedicato.
This commit is contained in:
39
vite-plugin-websocket.js
Normal file
39
vite-plugin-websocket.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import { WebSocketServer } from 'ws'
|
||||
import { setupWebSocketHandler } from './src/websocket-handler.js'
|
||||
import { printServerInfo } from './src/server-utils.js'
|
||||
|
||||
/**
|
||||
* Plugin Vite che integra un server WebSocket per la gestione dello stato di gioco.
|
||||
* @returns {import('vite').Plugin}
|
||||
*/
|
||||
export default function websocketPlugin() {
|
||||
return {
|
||||
name: 'vite-plugin-websocket',
|
||||
configureServer(server) {
|
||||
// Inizializza un server WebSocket collegato al server HTTP di Vite.
|
||||
// Importante: usa `noServer: true` per evitare conflitti con l'HMR di Vite.
|
||||
const wss = new WebSocketServer({ noServer: true })
|
||||
|
||||
// Registra i gestori WebSocket con la logica di gioco.
|
||||
setupWebSocketHandler(wss)
|
||||
|
||||
// Intercetta le richieste di upgrade WebSocket.
|
||||
server.httpServer.on('upgrade', (request, socket, head) => {
|
||||
// Se la richiesta non riguarda l'HMR di Vite (es. /@vite/client),
|
||||
// la gestisce il server WebSocket dell'applicazione.
|
||||
const pathname = new URL(request.url, `http://${request.headers.host}`).pathname
|
||||
|
||||
if (!pathname.startsWith('/@vite') && !pathname.startsWith('/@fs')) {
|
||||
wss.handleUpgrade(request, socket, head, (ws) => {
|
||||
wss.emit('connection', ws, request)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// Stampa le informazioni di accesso dopo l'avvio del server Vite.
|
||||
server.httpServer.once('listening', () => {
|
||||
setTimeout(() => printServerInfo(5173), 100)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user