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';
|
'ws://${host.replaceFirst(RegExp(r'^https?://'), '')}/ws?clientId=$clientId';
|
||||||
_wsChannel = WebSocketChannel.connect(Uri.parse(wsUrl));
|
_wsChannel = WebSocketChannel.connect(Uri.parse(wsUrl));
|
||||||
|
|
||||||
|
print('WebSocket connecting to $wsUrl');
|
||||||
|
|
||||||
_wsChannel!.stream.listen((message) {
|
_wsChannel!.stream.listen((message) {
|
||||||
final jsonData = jsonDecode(message);
|
final jsonData = jsonDecode(message);
|
||||||
|
|
||||||
@ -125,11 +127,26 @@ class WebSocketManager {
|
|||||||
}
|
}
|
||||||
}, onError: (error) {
|
}, onError: (error) {
|
||||||
print('WebSocket error: $error');
|
print('WebSocket error: $error');
|
||||||
|
_reconnect();
|
||||||
}, onDone: () {
|
}, onDone: () {
|
||||||
print('WebSocket connection closed');
|
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
|
/// Attempts to create a ProgressEvent from a WebSocketEvent
|
||||||
void _tryCreateProgressEvent(WebSocketEvent event) {
|
void _tryCreateProgressEvent(WebSocketEvent event) {
|
||||||
WebSocketEventHandler.tryCreateProgressEvent(
|
WebSocketEventHandler.tryCreateProgressEvent(
|
||||||
@ -170,6 +187,7 @@ class WebSocketManager {
|
|||||||
|
|
||||||
/// Closes the WebSocket connection and cleans up resources
|
/// Closes the WebSocket connection and cleans up resources
|
||||||
void dispose() {
|
void dispose() {
|
||||||
|
print('Disposing WebSocketManager...');
|
||||||
_wsChannel?.sink.close();
|
_wsChannel?.sink.close();
|
||||||
_progressController.close();
|
_progressController.close();
|
||||||
_eventController.close();
|
_eventController.close();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user