From dca26f114eab20031758f71c340eddeca18774f6 Mon Sep 17 00:00:00 2001 From: ramin-deriv Date: Wed, 20 Nov 2024 15:05:44 +0800 Subject: [PATCH] chore: move connect method inside the try-catch --- .../connection/api_manager/binary_api.dart | 6 ++- lib/state/connection/connection_cubit.dart | 38 +++++++++---------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/lib/services/connection/api_manager/binary_api.dart b/lib/services/connection/api_manager/binary_api.dart index 0aef7e0bcc..f7eaa161fa 100644 --- a/lib/services/connection/api_manager/binary_api.dart +++ b/lib/services/connection/api_manager/binary_api.dart @@ -131,8 +131,6 @@ class BinaryAPI extends BaseAPI { _webSocketChannel = IOWebSocketChannel.connect('$uri', pingInterval: _keepAlivePingInterval, customClient: client); - unawaited(_webSocketChannel?.ready.then((_) => _startConnectionTimer())); - _webSocketListener = _webSocketChannel?.stream .map?>((Object? result) => jsonDecode('$result')) .listen( @@ -162,6 +160,10 @@ class BinaryAPI extends BaseAPI { }, ); + await _webSocketChannel?.ready; + + _startConnectionTimer(); + _logDebugInfo('send initial message.'); } diff --git a/lib/state/connection/connection_cubit.dart b/lib/state/connection/connection_cubit.dart index e8d41c4596..eaf19f81d1 100644 --- a/lib/state/connection/connection_cubit.dart +++ b/lib/state/connection/connection_cubit.dart @@ -85,6 +85,25 @@ class ConnectionCubit extends Cubit { try { await _api.disconnect().timeout(_pingTimeout); + + await _api.connect( + _connectionInformation, + onOpen: (String key) { + if (_key == key) { + emit(const ConnectionConnectedState()); + } + }, + onDone: (String key) { + if (_key == key) { + unawaited(reconnect()); + } + }, + onError: (String key) { + if (_key == key) { + emit(const ConnectionDisconnectedState()); + } + }, + ).timeout(_pingTimeout); } on Exception catch (e) { dev.log('$runtimeType disconnect exception: $e', error: e); @@ -92,24 +111,5 @@ class ConnectionCubit extends Cubit { return; } - - await _api.connect( - _connectionInformation, - onOpen: (String key) { - if (_key == key) { - emit(const ConnectionConnectedState()); - } - }, - onDone: (String key) { - if (_key == key) { - unawaited(reconnect()); - } - }, - onError: (String key) { - if (_key == key) { - emit(const ConnectionDisconnectedState()); - } - }, - ); } }