feat(streaming): Improve socket reconnection and timeout handling

This commit is contained in:
cogwheel0
2025-11-29 15:02:00 +05:30
parent 4700301854
commit 99673db7fb
3 changed files with 225 additions and 10 deletions

View File

@@ -30,6 +30,13 @@ class SocketService with WidgetsBindingObserver {
final Map<String, _ChannelEventRegistration> _channelEventHandlers = {};
int _handlerSeed = 0;
/// Stream controller that emits when a socket reconnection occurs.
/// Listeners can use this to sync state after a reconnect.
final _reconnectController = StreamController<void>.broadcast();
/// Stream that emits when a socket reconnection occurs.
Stream<void> get onReconnect => _reconnectController.stream;
SocketService({
required this.serverConfig,
String? authToken,
@@ -214,6 +221,7 @@ class SocketService with WidgetsBindingObserver {
WidgetsBinding.instance.removeObserver(this);
_chatEventHandlers.clear();
_channelEventHandlers.clear();
_reconnectController.close();
}
// Best-effort: ensure there is an active connection and wait briefly.
@@ -279,6 +287,10 @@ class SocketService with WidgetsBindingObserver {
'auth': {'token': _authToken},
});
}
// Notify listeners that a reconnection occurred so they can refresh state
if (!_reconnectController.isClosed) {
_reconnectController.add(null);
}
}
void _handleConnectError(dynamic err) {}