@@ -4,7 +4,8 @@ import 'dart:developer' as dev;
44import 'dart:io' ;
55
66import 'package:flutter/widgets.dart' ;
7- import 'package:web_socket_channel/io.dart' ;
7+
8+ import 'package:web_socket_client/web_socket_client.dart' as ws;
89
910import 'package:flutter_deriv_api/api/models/enums.dart' ;
1011import 'package:flutter_deriv_api/basic_api/generated/forget_all_receive.dart' ;
@@ -14,6 +15,8 @@ import 'package:flutter_deriv_api/basic_api/response.dart';
1415import 'package:flutter_deriv_api/helpers/helpers.dart' ;
1516import 'package:flutter_deriv_api/services/connection/api_manager/base_api.dart' ;
1617import 'package:flutter_deriv_api/services/connection/api_manager/connection_information.dart' ;
18+ import 'package:flutter_deriv_api/services/connection/api_manager/enums.dart' ;
19+ import 'package:flutter_deriv_api/services/connection/api_manager/extensions.dart' ;
1720import 'package:flutter_deriv_api/services/connection/call_manager/base_call_manager.dart' ;
1821import 'package:flutter_deriv_api/services/connection/call_manager/call_history.dart' ;
1922import 'package:flutter_deriv_api/services/connection/call_manager/call_manager.dart' ;
@@ -26,13 +29,10 @@ class BinaryAPI extends BaseAPI {
2629 BinaryAPI ({String ? key, bool enableDebug = false })
2730 : super (key: key ?? '${UniqueKey ()}' , enableDebug: enableDebug);
2831
29- static const Duration _disconnectTimeOut = Duration (seconds: 5 );
30- static const Duration _websocketConnectTimeOut = Duration (seconds: 10 );
31-
3232 /// Represents the active websocket connection.
3333 ///
3434 /// This is used to send and receive data from the websocket server.
35- IOWebSocketChannel ? _webSocketChannel;
35+ ws. WebSocket ? _webSocketChannel;
3636
3737 /// Stream subscription to API data.
3838 StreamSubscription <Map <String , dynamic >?>? _webSocketListener;
@@ -76,12 +76,16 @@ class BinaryAPI extends BaseAPI {
7676 await _setUserAgent ();
7777
7878 // Initialize connection to websocket server.
79- _webSocketChannel = IOWebSocketChannel .connect (
80- '$uri ' ,
81- pingInterval: _websocketConnectTimeOut,
79+ _webSocketChannel = ws.WebSocket (
80+ uri,
81+ pingInterval: const Duration (seconds: 1 ),
82+ backoff: const ws.ConstantBackoff (Duration (seconds: 1 )),
8283 );
8384
84- _webSocketListener = _webSocketChannel? .stream
85+ await connectionStatus
86+ .firstWhere ((APIStatus status) => status == APIStatus .connected);
87+
88+ _webSocketListener = _webSocketChannel? .messages
8589 .map <Map <String , dynamic >?>((Object ? result) => jsonDecode ('$result ' ))
8690 .listen (
8791 (Map <String , dynamic >? message) {
@@ -91,10 +95,12 @@ class BinaryAPI extends BaseAPI {
9195 _handleResponse (message, printResponse: printResponse);
9296 }
9397 },
94- onDone: () async {
98+ onDone: () {
9599 _logDebugInfo ('the websocket is closed.' );
96100
97101 onDone? .call (key);
102+
103+ disconnect ();
98104 },
99105 onError: (Object error) {
100106 _logDebugInfo (
@@ -103,6 +109,8 @@ class BinaryAPI extends BaseAPI {
103109 );
104110
105111 onError? .call (key);
112+
113+ disconnect ();
106114 },
107115 );
108116
@@ -117,10 +125,9 @@ class BinaryAPI extends BaseAPI {
117125 @override
118126 void addToChannel (Map <String , dynamic > request) {
119127 try {
120- _webSocketChannel? .sink.add (utf8.encode (jsonEncode (request)));
121- // ignore: avoid_catches_without_on_clauses
122- } catch (error) {
123- _logDebugInfo ('error while adding to channel.' , error: error);
128+ _webSocketChannel? .send (utf8.encode (jsonEncode (request)));
129+ } on Exception catch (error) {
130+ _logDebugInfo ('error sending message to websocket.' , error: error);
124131 }
125132 }
126133
@@ -167,19 +174,23 @@ class BinaryAPI extends BaseAPI {
167174 try {
168175 await _webSocketListener? .cancel ();
169176
170- await _webSocketChannel? .sink.close ().timeout (
171- _disconnectTimeOut,
172- onTimeout: () => throw TimeoutException ('Could not close sink.' ),
173- );
174- // ignore: avoid_catches_without_on_clauses
175- } catch (e) {
177+ _webSocketChannel? .close ();
178+ } on Exception catch (e) {
176179 _logDebugInfo ('disconnect error.' , error: e);
177180 } finally {
178181 _webSocketListener = null ;
179182 _webSocketChannel = null ;
180183 }
181184 }
182185
186+ @override
187+ APIStatus get currentConnectionStatus =>
188+ _webSocketChannel! .connection.state.apiStatus;
189+
190+ @override
191+ Stream <APIStatus > get connectionStatus => _webSocketChannel! .connection
192+ .map <APIStatus >((ws.ConnectionState state) => state.apiStatus);
193+
183194 /// Handles responses that come from server, by using its reqId,
184195 /// and completes caller's Future or add the response to caller's stream if it was a subscription call.
185196 void _handleResponse (
0 commit comments