up
This commit is contained in:
+7
-5
@@ -79,16 +79,18 @@ try {
|
||||
error_log('SSE: Conexión en modo global (sin autenticación específica)');
|
||||
}
|
||||
|
||||
$db = Database::getInstance();
|
||||
|
||||
// Enviar evento inicial de conexión
|
||||
error_log("SSE: Nueva conexión establecida (userId: {$userId})");
|
||||
// IMPORTANTE: Enviar evento connected ANTES de hacer cualquier otra cosa
|
||||
error_log("SSE: Enviando evento connected (userId: {$userId})");
|
||||
sendSSEEvent('connected', [
|
||||
'timestamp' => time(),
|
||||
'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
|
||||
$lastEventId = $_SERVER['HTTP_LAST_EVENT_ID'] ?? null;
|
||||
$lastCheck = time();
|
||||
|
||||
@@ -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
@@ -1055,12 +1055,18 @@
|
||||
}
|
||||
|
||||
setupNotificationPolling() {
|
||||
// Carga inicial de notificaciones pendientes
|
||||
// Carga inicial de notificaciones pendientes (opcional)
|
||||
// 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)
|
||||
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' });
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if (resp.status === 401) {
|
||||
console.warn('Notifications fetch: unauthorized (session may have expired)');
|
||||
console.debug('Notifications fetch: unauthorized (SSE manejará las notificaciones)');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1308,10 +1315,11 @@
|
||||
if (json && json.success && Array.isArray(json.data)) {
|
||||
json.data.forEach(n => this.showNotificationToast(n));
|
||||
} 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) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user