implement socket heartbeat

This commit is contained in:
2025-05-27 16:12:15 +02:00
parent e9b1fb6577
commit 45b3fb0cd6
3 changed files with 31 additions and 11 deletions

View File

@@ -258,6 +258,12 @@ def on_leave(data):
conversation_id = data.get('conversation_id') conversation_id = data.get('conversation_id')
leave_room(f'conversation_{conversation_id}') leave_room(f'conversation_{conversation_id}')
@socketio.on('heartbeat')
@login_required
def on_heartbeat(data):
# Just acknowledge the heartbeat to keep the connection alive
return {'status': 'ok'}
@conversations_bp.route('/<int:conversation_id>/send_message', methods=['POST']) @conversations_bp.route('/<int:conversation_id>/send_message', methods=['POST'])
@login_required @login_required
@require_password_change @require_password_change

View File

@@ -412,7 +412,12 @@ if (typeof window.ChatManager === 'undefined') {
timeout: 20000, timeout: 20000,
autoConnect: true, autoConnect: true,
forceNew: true, forceNew: true,
multiplex: false multiplex: false,
pingTimeout: 60000,
pingInterval: 25000,
upgradeTimeout: 10000,
rememberUpgrade: true,
rejectUnauthorized: false
}); });
// Set up socket event handlers // Set up socket event handlers
@@ -426,15 +431,14 @@ if (typeof window.ChatManager === 'undefined') {
existingSocketId: state.connectionState.socketId existingSocketId: state.connectionState.socketId
}); });
// Rejoin conversation room on reconnection // Always rejoin the room on connect
if (state.connectionState.hasJoined) { console.log('Joining conversation room:', conversationId);
console.log('Rejoining conversation room:', conversationId);
socket.emit('join_conversation', { socket.emit('join_conversation', {
conversation_id: conversationId, conversation_id: conversationId,
timestamp: new Date().toISOString(), timestamp: new Date().toISOString(),
socketId: socket.id socketId: socket.id
}); });
} state.connectionState.hasJoined = true;
}); });
socket.on('disconnect', function(reason) { socket.on('disconnect', function(reason) {
@@ -445,7 +449,7 @@ if (typeof window.ChatManager === 'undefined') {
}); });
state.connectionState.isConnected = false; state.connectionState.isConnected = false;
state.connectionState.socketId = null; state.connectionState.socketId = null;
// Don't set hasJoined to false to allow reconnection // Don't set hasJoined to false to maintain room membership
}); });
socket.on('connect_error', function(error) { socket.on('connect_error', function(error) {
@@ -456,6 +460,16 @@ if (typeof window.ChatManager === 'undefined') {
console.error('Socket error:', error); console.error('Socket error:', error);
}); });
// Add heartbeat to keep connection alive
setInterval(function() {
if (socket.connected) {
socket.emit('heartbeat', {
timestamp: new Date().toISOString(),
socketId: socket.id
});
}
}, 15000);
instance = { instance = {
socket: socket, socket: socket,
state: state, state: state,