up
This commit is contained in:
@@ -1034,6 +1034,8 @@
|
||||
this._stagedMessages = [];
|
||||
// Last message timestamp seen (for delta polling)
|
||||
this._latestMessage = null;
|
||||
// SSE connection handle for server push (EventSource)
|
||||
this._sse = null;
|
||||
// Removed: automatic "Nuevos mensajes" indicator — we always reload full conversation now
|
||||
// this._lastRenderMessageCount = 0;
|
||||
|
||||
@@ -1898,6 +1900,50 @@
|
||||
}, 10000);
|
||||
}
|
||||
|
||||
// SSE: start listening for server-sent events for the current conversation
|
||||
startSSE(userId) {
|
||||
try {
|
||||
this.stopSSE();
|
||||
if (!userId || typeof EventSource === 'undefined') return;
|
||||
const url = `api/stream_user_messages.php?user_id=${userId}`;
|
||||
try {
|
||||
this._sse = new EventSource(url);
|
||||
this._sse.onmessage = (ev) => {
|
||||
try {
|
||||
const d = JSON.parse(ev.data || '{}');
|
||||
if (d && d.type === 'new_message' && d.user_id == this.currentUserId) {
|
||||
if (console && console.debug) console.debug('SSE new_message received for user', d.user_id, '-> triggering full load');
|
||||
if (!this._fullLoadInProgress) {
|
||||
this.loadMessages(this.currentUserId, true, true).catch(e => console.warn('SSE triggered load failed', e));
|
||||
} else {
|
||||
if (console && console.debug) console.debug('SSE: full load already in progress, skipping immediate load');
|
||||
}
|
||||
}
|
||||
} catch (e) { console.warn('SSE message parse failed', e); }
|
||||
};
|
||||
this._sse.addEventListener('ping', () => {/* no-op */});
|
||||
this._sse.onerror = (err) => {
|
||||
console.warn('SSE error', err);
|
||||
// Close and attempt reconnect after short delay
|
||||
try { this._sse.close(); } catch(e) {}
|
||||
this._sse = null;
|
||||
setTimeout(() => { if (this.currentUserId == userId) this.startSSE(userId); }, 3000);
|
||||
};
|
||||
if (console && console.debug) console.debug('SSE started for user', userId);
|
||||
} catch (e) { console.warn('Failed to start EventSource', e); }
|
||||
} catch (e) { console.warn('startSSE failed', e); }
|
||||
}
|
||||
|
||||
stopSSE() {
|
||||
try {
|
||||
if (this._sse) {
|
||||
try { this._sse.close(); } catch(e) {}
|
||||
this._sse = null;
|
||||
if (console && console.debug) console.debug('SSE stopped');
|
||||
}
|
||||
} catch (e) { /* ignore */ }
|
||||
}
|
||||
|
||||
// New: periodic delta poller to fetch only messages newer than last seen timestamp
|
||||
async pollNewMessages() {
|
||||
if (!this.currentUserId) return;
|
||||
@@ -2257,6 +2303,8 @@
|
||||
|
||||
async openConversation(userId, userName, phoneNumber) {
|
||||
this.currentUserId = userId;
|
||||
// Stop any previous SSE connection before opening a new conversation
|
||||
try { this.stopSSE(); } catch(e) { /* ignore */ }
|
||||
// Clear any staged messages from a previous conversation and hide indicator
|
||||
try { if (this._stagedMessageIds) { this._stagedMessageIds.clear(); this._stagedCount = 0; this.hideNewMessagesIndicator(); } } catch(e) {}
|
||||
|
||||
@@ -2592,6 +2640,9 @@
|
||||
// Resume auto-refresh after the load
|
||||
try { this._suspendAutoRefresh = false; } catch(e) {}
|
||||
|
||||
// Start SSE connection for this conversation to receive server push events
|
||||
try { this.startSSE(userId); } catch(e) { console.warn('startSSE failed to initialize', e); }
|
||||
|
||||
// Añadir listener para scroll arriba (cargar más historial)
|
||||
const chatContainer = document.getElementById('chat-conversations');
|
||||
if (chatContainer) {
|
||||
|
||||
Reference in New Issue
Block a user