This commit is contained in:
Lizandro Guarnizo
2026-01-27 15:01:32 -05:00
parent 1f224044f7
commit 6e3f375f91
3 changed files with 30 additions and 12 deletions
+7 -5
View File
@@ -79,16 +79,18 @@ try {
error_log('SSE: Conexión en modo global (sin autenticación específica)'); error_log('SSE: Conexión en modo global (sin autenticación específica)');
} }
$db = Database::getInstance(); // IMPORTANTE: Enviar evento connected ANTES de hacer cualquier otra cosa
error_log("SSE: Enviando evento connected (userId: {$userId})");
// Enviar evento inicial de conexión
error_log("SSE: Nueva conexión establecida (userId: {$userId})");
sendSSEEvent('connected', [ sendSSEEvent('connected', [
'timestamp' => time(), 'timestamp' => time(),
'user_id' => $userId, 'user_id' => $userId,
'mode' => $authenticated ? 'authenticated' : 'global' 'mode' => $authenticated ? 'authenticated' : 'global',
'status' => 'ready'
]); ]);
// Ahora sí, conectar a BD
$db = Database::getInstance();
// Obtener último timestamp conocido por el cliente // Obtener último timestamp conocido por el cliente
$lastEventId = $_SERVER['HTTP_LAST_EVENT_ID'] ?? null; $lastEventId = $_SERVER['HTTP_LAST_EVENT_ID'] ?? null;
$lastCheck = time(); $lastCheck = time();
+8
View File
@@ -0,0 +1,8 @@
<?php
// Test simplísimo de SSE
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
echo "event: test\n";
echo "data: {\"message\":\"works\"}\n\n";
flush();
+15 -7
View File
@@ -1055,12 +1055,18 @@
} }
setupNotificationPolling() { setupNotificationPolling() {
// Carga inicial de notificaciones pendientes // Carga inicial de notificaciones pendientes (opcional)
// SSE se encargará de las nuevas en tiempo real // SSE se encargará de las nuevas en tiempo real
this.loadNotifications(); this.loadNotifications().catch(e => {
console.debug('Carga inicial de notificaciones omitida (SSE activo):', e.message);
});
// Backup: verificar cada 60 segundos (solo por si SSE falla) // Backup: verificar cada 60 segundos (solo por si SSE falla)
setInterval(() => this.loadNotifications(), 60000); setInterval(() => {
this.loadNotifications().catch(e => {
console.debug('Polling de notificaciones omitido (SSE activo)');
});
}, 60000);
} }
/** /**
@@ -1294,12 +1300,13 @@
const resp = await fetch('api/get_notifications.php', { credentials: 'same-origin' }); const resp = await fetch('api/get_notifications.php', { credentials: 'same-origin' });
if (!resp.ok) { if (!resp.ok) {
console.warn(`Notifications fetch: HTTP ${resp.status} ${resp.statusText}`); // Si falla, no es problema: SSE manejará las notificaciones en tiempo real
console.debug(`Notifications fetch: HTTP ${resp.status} (SSE manejará las notificaciones)`);
return; return;
} }
if (resp.status === 401) { if (resp.status === 401) {
console.warn('Notifications fetch: unauthorized (session may have expired)'); console.debug('Notifications fetch: unauthorized (SSE manejará las notificaciones)');
return; return;
} }
@@ -1308,10 +1315,11 @@
if (json && json.success && Array.isArray(json.data)) { if (json && json.success && Array.isArray(json.data)) {
json.data.forEach(n => this.showNotificationToast(n)); json.data.forEach(n => this.showNotificationToast(n));
} else if (json && json.success === false) { } else if (json && json.success === false) {
console.warn('get_notifications returned error:', json.error || json); console.debug('get_notifications returned error (no crítico, SSE activo):', json.error || json);
} }
} catch (e) { } catch (e) {
console.error('Error loading notifications', e); // Error no crítico: SSE manejará las notificaciones en tiempo real
console.debug('loadNotifications failed (SSE manejará las notificaciones):', e.message);
} }
} }