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')
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'])
@login_required
@require_password_change

View File

@@ -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);
// 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,