fix(voce): riproduce la sintesi vocale sul display invece che sul controller

Il controller invia un comando 'speak' via WebSocket. Il server inoltra il messaggio solo ai client display, che eseguono speechSynthesis con preferenza per voce italiana.
This commit is contained in:
2026-02-11 19:35:09 +01:00
parent 43194c4fbe
commit 581a567c17
3 changed files with 67 additions and 8 deletions
+28
View File
@@ -28,6 +28,10 @@ export function setupWebSocketHandler(wss) {
if (msg.type === 'action') {
return handleAction(ws, msg)
}
if (msg.type === 'speak') {
return handleSpeak(ws, msg)
}
} catch (err) {
console.error('Error processing message:', err, 'data:', data)
// Invia l'errore solo se la connessione e ancora aperta.
@@ -98,6 +102,30 @@ export function setupWebSocketHandler(wss) {
broadcastState()
}
/**
* Gestisce una richiesta di sintesi vocale proveniente dal controller.
* Il messaggio viene inoltrato solo ai client registrati come display.
*/
function handleSpeak(ws, msg) {
const client = clients.get(ws)
if (!client || client.role !== 'controller') {
sendError(ws, 'Only controllers can request speech')
return
}
if (typeof msg.text !== 'string' || msg.text.trim() === '') {
sendError(ws, 'Invalid speak payload')
return
}
const speakMsg = JSON.stringify({ type: 'speak', text: msg.text.trim() })
clients.forEach((meta, clientWs) => {
if (meta.role === 'display' && clientWs.readyState === 1) {
clientWs.send(speakMsg)
}
})
}
/**
* Invia un messaggio di errore al client
*/