@@ -4,7 +4,8 @@ import 'dart:developer' as dev;
4
4
import 'dart:io' ;
5
5
6
6
import '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;
8
9
9
10
import 'package:flutter_deriv_api/api/models/enums.dart' ;
10
11
import 'package:flutter_deriv_api/basic_api/generated/forget_all_receive.dart' ;
@@ -14,6 +15,8 @@ import 'package:flutter_deriv_api/basic_api/response.dart';
14
15
import 'package:flutter_deriv_api/helpers/helpers.dart' ;
15
16
import 'package:flutter_deriv_api/services/connection/api_manager/base_api.dart' ;
16
17
import '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' ;
17
20
import 'package:flutter_deriv_api/services/connection/call_manager/base_call_manager.dart' ;
18
21
import 'package:flutter_deriv_api/services/connection/call_manager/call_history.dart' ;
19
22
import 'package:flutter_deriv_api/services/connection/call_manager/call_manager.dart' ;
@@ -26,13 +29,10 @@ class BinaryAPI extends BaseAPI {
26
29
BinaryAPI ({String ? key, bool enableDebug = false })
27
30
: super (key: key ?? '${UniqueKey ()}' , enableDebug: enableDebug);
28
31
29
- static const Duration _disconnectTimeOut = Duration (seconds: 5 );
30
- static const Duration _websocketConnectTimeOut = Duration (seconds: 10 );
31
-
32
32
/// Represents the active websocket connection.
33
33
///
34
34
/// This is used to send and receive data from the websocket server.
35
- IOWebSocketChannel ? _webSocketChannel;
35
+ ws. WebSocket ? _webSocketChannel;
36
36
37
37
/// Stream subscription to API data.
38
38
StreamSubscription <Map <String , dynamic >?>? _webSocketListener;
@@ -76,12 +76,16 @@ class BinaryAPI extends BaseAPI {
76
76
await _setUserAgent ();
77
77
78
78
// 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 )),
82
83
);
83
84
84
- _webSocketListener = _webSocketChannel? .stream
85
+ await connectionStatus
86
+ .firstWhere ((APIStatus status) => status == APIStatus .connected);
87
+
88
+ _webSocketListener = _webSocketChannel? .messages
85
89
.map <Map <String , dynamic >?>((Object ? result) => jsonDecode ('$result ' ))
86
90
.listen (
87
91
(Map <String , dynamic >? message) {
@@ -91,10 +95,12 @@ class BinaryAPI extends BaseAPI {
91
95
_handleResponse (message, printResponse: printResponse);
92
96
}
93
97
},
94
- onDone: () async {
98
+ onDone: () {
95
99
_logDebugInfo ('the websocket is closed.' );
96
100
97
101
onDone? .call (key);
102
+
103
+ disconnect ();
98
104
},
99
105
onError: (Object error) {
100
106
_logDebugInfo (
@@ -103,6 +109,8 @@ class BinaryAPI extends BaseAPI {
103
109
);
104
110
105
111
onError? .call (key);
112
+
113
+ disconnect ();
106
114
},
107
115
);
108
116
@@ -117,10 +125,9 @@ class BinaryAPI extends BaseAPI {
117
125
@override
118
126
void addToChannel (Map <String , dynamic > request) {
119
127
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);
124
131
}
125
132
}
126
133
@@ -167,19 +174,23 @@ class BinaryAPI extends BaseAPI {
167
174
try {
168
175
await _webSocketListener? .cancel ();
169
176
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) {
176
179
_logDebugInfo ('disconnect error.' , error: e);
177
180
} finally {
178
181
_webSocketListener = null ;
179
182
_webSocketChannel = null ;
180
183
}
181
184
}
182
185
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
+
183
194
/// Handles responses that come from server, by using its reqId,
184
195
/// and completes caller's Future or add the response to caller's stream if it was a subscription call.
185
196
void _handleResponse (
0 commit comments