Enhance WebSocketManager with reconnection logic and debug prints

This commit is contained in:
Menno van Leeuwen 2025-03-21 11:50:48 +01:00
parent c1c508215e
commit f45572532f
Signed by: vleeuwenmenno
SSH Key Fingerprint: SHA256:OJFmjANpakwD3F2Rsws4GLtbdz1TJ5tkQF0RZmF0TRE

View File

@ -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();