Enhance WebSocketManager with reconnection logic and debug prints
This commit is contained in:
parent
c1c508215e
commit
f45572532f
@ -78,6 +78,8 @@ class WebSocketManager {
|
||||
'ws://${host.replaceFirst(RegExp(r'^https?://'), '')}/ws?clientId=$clientId';
|
||||
_wsChannel = WebSocketChannel.connect(Uri.parse(wsUrl));
|
||||
|
||||
print('WebSocket connecting to $wsUrl');
|
||||
|
||||
_wsChannel!.stream.listen((message) {
|
||||
final jsonData = jsonDecode(message);
|
||||
|
||||
@ -125,11 +127,26 @@ class WebSocketManager {
|
||||
}
|
||||
}, onError: (error) {
|
||||
print('WebSocket error: $error');
|
||||
_reconnect();
|
||||
}, onDone: () {
|
||||
print('WebSocket connection closed');
|
||||
_reconnect();
|
||||
});
|
||||
}
|
||||
|
||||
/// Reconnects the WebSocket with a delay
|
||||
Future<void> _reconnect() async {
|
||||
print('Attempting to reconnect WebSocket in 5 seconds...');
|
||||
await Future.delayed(Duration(seconds: 5));
|
||||
try {
|
||||
await connect();
|
||||
print('WebSocket reconnected successfully');
|
||||
} catch (e) {
|
||||
print('WebSocket reconnection failed: $e');
|
||||
_reconnect(); // Retry again if reconnection fails
|
||||
}
|
||||
}
|
||||
|
||||
/// Attempts to create a ProgressEvent from a WebSocketEvent
|
||||
void _tryCreateProgressEvent(WebSocketEvent event) {
|
||||
WebSocketEventHandler.tryCreateProgressEvent(
|
||||
@ -170,6 +187,7 @@ class WebSocketManager {
|
||||
|
||||
/// Closes the WebSocket connection and cleans up resources
|
||||
void dispose() {
|
||||
print('Disposing WebSocketManager...');
|
||||
_wsChannel?.sink.close();
|
||||
_progressController.close();
|
||||
_eventController.close();
|
||||
|
Loading…
x
Reference in New Issue
Block a user