From c2c03efe9e25191e77e947d17844a7e763b7fa6d Mon Sep 17 00:00:00 2001 From: Kobe Date: Tue, 27 May 2025 15:29:31 +0200 Subject: [PATCH] socketIO fix --- __pycache__/extensions.cpython-313.pyc | Bin 493 -> 609 bytes entrypoint.sh | 2 +- extensions.py | 9 +++- requirements.txt | 5 ++- templates/conversations/conversation.html | 50 +++++++++++++++------- 5 files changed, 48 insertions(+), 18 deletions(-) diff --git a/__pycache__/extensions.cpython-313.pyc b/__pycache__/extensions.cpython-313.pyc index ac913ccabdb2be3c97e49c01d0c73929082ec116..05104fce52d7b0844d588672aa2a6e22bf6227c5 100644 GIT binary patch delta 246 zcmaFM{E$WcGcPX}0}#C2Ynri^k%8echyw$hP{!wyiR$S>!R$cD5zHCPWyw~=ozAVv zGjW<@{VmRtjH1-Ul+3*Jke4<<4KF2ugeKc9k>vcM;`qdzoc!|Cl=%Fj%=FB>;#*va z#g%!<@wxdaskhj2^3&5(i*E6!<^knWGxOu20z3sk)8b1qb5rw6OK$Okd6{`7sYPXp zIewZvxA@X>5{t9ri}RDSC;nT_B?Gh&=yZl+m&uzM_2h0y%G}_Wy1~tVLqy_+pzsYL Qk&nFWJWP!oMN&XT04@?uPXGV_ delta 116 zcmaFJ@|IcsGcPX}0}$vYnPliOGB7*_abSQ2%J`f$Q9Yf*lCg+8on4b-;sQqrO~zXy z$@xXa@rgM(`Q@o8@%crW>6v-OlLHxdaftx60gYlPHkvHTq{p*D_#(G%BS(=CPzV61 C(i%Yk diff --git a/entrypoint.sh b/entrypoint.sh index 38fb14e..1051690 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -52,4 +52,4 @@ init_admin() # Start the application echo "Starting application..." -exec gunicorn --bind 0.0.0.0:5000 app:app \ No newline at end of file +exec gunicorn --worker-class eventlet -w 1 --bind 0.0.0.0:5000 app:app \ No newline at end of file diff --git a/extensions.py b/extensions.py index 6396315..a965a67 100644 --- a/extensions.py +++ b/extensions.py @@ -7,4 +7,11 @@ from flask_wtf.csrf import CSRFProtect db = SQLAlchemy() login_manager = LoginManager() csrf = CSRFProtect() -socketio = SocketIO(cors_allowed_origins="*") \ No newline at end of file +socketio = SocketIO( + cors_allowed_origins="*", + async_mode='threading', + logger=True, + engineio_logger=True, + ping_timeout=60, + ping_interval=25 +) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 27949a6..9b2377a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,4 +10,7 @@ python-dotenv==1.0.1 psycopg2-binary==2.9.9 gunicorn==21.2.0 Flask-SocketIO==5.3.6 -email_validator==2.1.0.post1 \ No newline at end of file +python-socketio==5.10.0 +python-engineio==4.8.0 +email_validator==2.1.0.post1 +simple-websocket==1.0.0 \ No newline at end of file diff --git a/templates/conversations/conversation.html b/templates/conversations/conversation.html index ee77acd..e4c31d1 100644 --- a/templates/conversations/conversation.html +++ b/templates/conversations/conversation.html @@ -399,11 +399,14 @@ if (typeof window.ChatManager === 'undefined') { // Create socket instance socket = io({ - transports: ['websocket'], - upgrade: false, - reconnection: false, - debug: true, - forceNew: false, + transports: ['websocket', 'polling'], // Allow fallback to polling + upgrade: true, // Enable transport upgrade + reconnection: true, // Enable reconnection + reconnectionAttempts: 5, // Number of reconnection attempts + reconnectionDelay: 1000, // Delay between reconnection attempts + timeout: 20000, // Connection timeout + debug: false, // Disable debug in production + forceNew: true, // Force new connection multiplex: false }); @@ -419,20 +422,37 @@ if (typeof window.ChatManager === 'undefined') { }); }); - socket.on('disconnect', function(reason) { - console.log('Disconnected from conversation:', { - reason: reason, - socketId: socket.id, - connectionState: state.connectionState - }); + socket.on('connect_error', function(error) { + console.error('Socket connection error:', error); state.connectionState.isConnected = false; state.connectionState.hasJoined = false; - state.connectionState.socketId = null; }); - socket.on('error', function(error) { - console.error('Socket error:', error); - cleanup(); + socket.on('reconnect_attempt', function(attemptNumber) { + console.log('Attempting to reconnect:', attemptNumber); + }); + + socket.on('reconnect', function(attemptNumber) { + console.log('Reconnected after', attemptNumber, 'attempts'); + state.connectionState.isConnected = true; + // Rejoin the conversation room after reconnection + if (!state.connectionState.hasJoined) { + socket.emit('join_conversation', { + conversation_id: conversationId, + timestamp: new Date().toISOString(), + socketId: socket.id + }); + state.connectionState.hasJoined = true; + } + }); + + socket.on('reconnect_error', function(error) { + console.error('Reconnection error:', error); + }); + + socket.on('reconnect_failed', function() { + console.error('Failed to reconnect'); + alert('Connection lost. Please refresh the page.'); }); instance = {