diff --git a/routes/__pycache__/conversations.cpython-313.pyc b/routes/__pycache__/conversations.cpython-313.pyc index e1787f4..da68000 100644 Binary files a/routes/__pycache__/conversations.cpython-313.pyc and b/routes/__pycache__/conversations.cpython-313.pyc differ diff --git a/routes/conversations.py b/routes/conversations.py index c5e0554..56c389c 100644 --- a/routes/conversations.py +++ b/routes/conversations.py @@ -258,6 +258,12 @@ def on_leave(data): conversation_id = data.get('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('//send_message', methods=['POST']) @login_required @require_password_change diff --git a/templates/conversations/conversation.html b/templates/conversations/conversation.html index 555d832..6bce0fd 100644 --- a/templates/conversations/conversation.html +++ b/templates/conversations/conversation.html @@ -412,7 +412,12 @@ if (typeof window.ChatManager === 'undefined') { timeout: 20000, autoConnect: true, forceNew: true, - multiplex: false + multiplex: false, + pingTimeout: 60000, + pingInterval: 25000, + upgradeTimeout: 10000, + rememberUpgrade: true, + rejectUnauthorized: false }); // Set up socket event handlers @@ -426,15 +431,14 @@ if (typeof window.ChatManager === 'undefined') { existingSocketId: state.connectionState.socketId }); - // Rejoin conversation room on reconnection - if (state.connectionState.hasJoined) { - console.log('Rejoining conversation room:', conversationId); - socket.emit('join_conversation', { - conversation_id: conversationId, - timestamp: new Date().toISOString(), - socketId: socket.id - }); - } + // Always rejoin the room on connect + console.log('Joining conversation room:', conversationId); + socket.emit('join_conversation', { + conversation_id: conversationId, + timestamp: new Date().toISOString(), + socketId: socket.id + }); + state.connectionState.hasJoined = true; }); socket.on('disconnect', function(reason) { @@ -445,7 +449,7 @@ if (typeof window.ChatManager === 'undefined') { }); state.connectionState.isConnected = false; 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) { @@ -456,6 +460,16 @@ if (typeof window.ChatManager === 'undefined') { 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 = { socket: socket, state: state,