fix
This commit is contained in:
@@ -20,7 +20,6 @@ class SocketService {
|
|||||||
io.Socket? _socket;
|
io.Socket? _socket;
|
||||||
bool _isConnected = false;
|
bool _isConnected = false;
|
||||||
bool _intentionalDisconnect = false;
|
bool _intentionalDisconnect = false;
|
||||||
bool _isReconnecting = false;
|
|
||||||
int _reconnectAttempts = 0;
|
int _reconnectAttempts = 0;
|
||||||
static const _maxReconnectAttempts = 3;
|
static const _maxReconnectAttempts = 3;
|
||||||
String? _currentToken;
|
String? _currentToken;
|
||||||
@@ -84,9 +83,11 @@ class SocketService {
|
|||||||
_socket!.onConnect((_) {
|
_socket!.onConnect((_) {
|
||||||
_log.i('Socket connected: ${_socket!.id}');
|
_log.i('Socket connected: ${_socket!.id}');
|
||||||
_isConnected = true;
|
_isConnected = true;
|
||||||
_reconnectAttempts = 0; // Reset on successful connect
|
|
||||||
_isReconnecting = false;
|
|
||||||
_connectionController.add(true);
|
_connectionController.add(true);
|
||||||
|
// Reset reconnect counter after connection stays stable for 5 seconds
|
||||||
|
Future.delayed(const Duration(seconds: 5), () {
|
||||||
|
if (_isConnected) _reconnectAttempts = 0;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
_socket!.onDisconnect((reason) {
|
_socket!.onDisconnect((reason) {
|
||||||
@@ -94,13 +95,14 @@ class SocketService {
|
|||||||
_isConnected = false;
|
_isConnected = false;
|
||||||
_connectionController.add(false);
|
_connectionController.add(false);
|
||||||
|
|
||||||
if (reason == 'io server disconnect' && !_intentionalDisconnect && !_isReconnecting) {
|
if (reason == 'io server disconnect' && !_intentionalDisconnect) {
|
||||||
if (_reconnectAttempts < _maxReconnectAttempts) {
|
if (_reconnectAttempts < _maxReconnectAttempts) {
|
||||||
_reconnectAttempts++;
|
_reconnectAttempts++;
|
||||||
_log.w('Server rejected connection (attempt $_reconnectAttempts/$_maxReconnectAttempts) - refreshing token...');
|
_log.w('Server rejected connection (attempt $_reconnectAttempts/$_maxReconnectAttempts) - refreshing token...');
|
||||||
_reconnectWithFreshToken();
|
_reconnectWithFreshToken();
|
||||||
} else {
|
} else {
|
||||||
_log.e('Max reconnect attempts reached - giving up');
|
_log.e('Max reconnect attempts reached - giving up. Check backend logs for rejection reason.');
|
||||||
|
_reconnectAttempts = 0; // Reset for future manual reconnects
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -180,7 +182,6 @@ class SocketService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _reconnectWithFreshToken() async {
|
Future<void> _reconnectWithFreshToken() async {
|
||||||
_isReconnecting = true;
|
|
||||||
// Wait with exponential backoff
|
// Wait with exponential backoff
|
||||||
final delay = Duration(seconds: _reconnectAttempts * 2);
|
final delay = Duration(seconds: _reconnectAttempts * 2);
|
||||||
_log.i('Waiting ${delay.inSeconds}s before reconnect attempt...');
|
_log.i('Waiting ${delay.inSeconds}s before reconnect attempt...');
|
||||||
@@ -236,7 +237,6 @@ class SocketService {
|
|||||||
|
|
||||||
void disconnect() {
|
void disconnect() {
|
||||||
_intentionalDisconnect = true;
|
_intentionalDisconnect = true;
|
||||||
_isReconnecting = false;
|
|
||||||
_reconnectAttempts = 0;
|
_reconnectAttempts = 0;
|
||||||
_socket?.disconnect();
|
_socket?.disconnect();
|
||||||
_socket?.dispose();
|
_socket?.dispose();
|
||||||
|
|||||||
Reference in New Issue
Block a user