seguimos intentado hacer funcionar el realtime
This commit is contained in:
@@ -7,23 +7,30 @@ export function registerSse(app) {
|
|||||||
res.set({
|
res.set({
|
||||||
'Content-Type': 'text/event-stream',
|
'Content-Type': 'text/event-stream',
|
||||||
'Cache-Control': 'no-cache',
|
'Cache-Control': 'no-cache',
|
||||||
'Connection': 'keep-alive'
|
Connection: 'keep-alive'
|
||||||
});
|
});
|
||||||
|
|
||||||
res.flushHeaders();
|
res.flushHeaders();
|
||||||
res.write('event: connected\ndata: {}\n\n'); // ← mantiene el stream vivo
|
|
||||||
|
// Primer “hola”
|
||||||
|
res.write('event: connected\ndata: {}\n\n');
|
||||||
|
|
||||||
|
// 🔴 Mantén viva la conexión con pings cada 15 s
|
||||||
|
const keepAlive = setInterval(() => {
|
||||||
|
res.write(':\n\n'); // comentario SSE = ping
|
||||||
|
}, 15000);
|
||||||
|
|
||||||
sseClients.push(res);
|
sseClients.push(res);
|
||||||
console.log(`🟢 Cliente SSE conectado (${sseClients.length})`);
|
console.log('🟢 Cliente SSE conectado (%d)', sseClients.length);
|
||||||
|
|
||||||
req.on('close', () => {
|
req.on('close', () => {
|
||||||
const idx = sseClients.indexOf(res);
|
clearInterval(keepAlive); // limpia el intervalo
|
||||||
if (idx !== -1) sseClients.splice(idx, 1);
|
sseClients.splice(sseClients.indexOf(res), 1);
|
||||||
console.log(`🔌 Cliente SSE desconectado (${sseClients.length})`);
|
console.log('🔌 Cliente SSE desconectado (%d)', sseClients.length);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const broadcast = (data) => {
|
const broadcast = (data) => {
|
||||||
const payload = `data: ${data}\n\n`;
|
const payload = `data: ${data}\n\n`;
|
||||||
sseClients.forEach((client) => client.write(payload));
|
sseClients.forEach((client) => client.write(payload));
|
||||||
|
|||||||
Reference in New Issue
Block a user