Skip to content

Commit

Permalink
chore: move connect method inside the try-catch
Browse files Browse the repository at this point in the history
  • Loading branch information
ramin-deriv committed Nov 20, 2024
1 parent 77e6a08 commit dca26f1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
6 changes: 4 additions & 2 deletions lib/services/connection/api_manager/binary_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map<String, dynamic>?>((Object? result) => jsonDecode('$result'))
.listen(
Expand Down Expand Up @@ -162,6 +160,10 @@ class BinaryAPI extends BaseAPI {
},
);

await _webSocketChannel?.ready;

_startConnectionTimer();

_logDebugInfo('send initial message.');
}

Expand Down
38 changes: 19 additions & 19 deletions lib/state/connection/connection_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,31 +85,31 @@ class ConnectionCubit extends Cubit<ConnectionState> {

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

unawaited(reconnect());

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());
}
},
);
}
}

0 comments on commit dca26f1

Please sign in to comment.