From 45b3fb0cd6090e5a1bd7a338ed705e42e5f843dd Mon Sep 17 00:00:00 2001 From: Kobe Date: Tue, 27 May 2025 16:12:15 +0200 Subject: [PATCH] implement socket heartbeat --- .../__pycache__/conversations.cpython-313.pyc | Bin 21967 -> 22200 bytes routes/conversations.py | 6 +++ templates/conversations/conversation.html | 36 ++++++++++++------ 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/routes/__pycache__/conversations.cpython-313.pyc b/routes/__pycache__/conversations.cpython-313.pyc index e1787f47a29b66d43555c74728072afd7b6b7606..da68000b71aca98798aeae33ab3472fc0f596b89 100644 GIT binary patch delta 718 zcmYL`O=uHA7>0Ltvq_U}lOlv9jYV4(Sras|mbO%ygd8fCrnHMEqhU>gt;PmsHx<1o zJv1tUKc9klEjf#l&^4(ZO}O?`Eo! zziRQ*w8(;2_d(d}F`CC}_Fd~ZKq$~$$wn_QP0O$`cGmiqNqGRm2^rFneo}#@NRiaR z8SN!y2u34h9dgk??>p?kTC+QypI^vQP1Wa^o6H=gb$AwC6o0d%WQ@knSfx4^UwgI!%7S=*+|r{u_!U_* zK2ykL$EcfS+2XVz~0Ah-;L;LSlAH`om%+=G-kVp delta 536 zcmdn7mht>*M!wIyyj%=G@cfu*#v`YRd=iXt8`WnqPiABi!@`bV;b6~rda{P2w(Jp*;=>@_0?GM# zWvNBQi6xo&dGRTkMMbwK7dSR?e*;Rg0L1we0lQkRnZICvQ4~kAsj`vU! zy$oVr0TCdxih4lA=gHGNbXflanUR~1d+cWoJqQv#1SD?p=Oh-F#OJ0K7bm8t#%HEz zG8UZzNlpe4XF$YR5OEGf#Dj?QAmRduxCkOHfrzUh;`-#GjW8C=OUSv+hZ03slqh5!Hn 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,