Skip to content

Commit 0119c7e

Browse files
committed
hamed/refactor_ws_in_deriv_api
hamed/refactor_ws_in_deriv_api
1 parent ddc27f4 commit 0119c7e

File tree

16 files changed

+181
-178
lines changed

16 files changed

+181
-178
lines changed

binary-websocket-api

example/ios/Flutter/AppFrameworkInfo.plist

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
<key>CFBundleVersion</key>
2222
<string>1.0</string>
2323
<key>MinimumOSVersion</key>
24-
<string>8.0</string>
24+
<string>11.0</string>
2525
</dict>
2626
</plist>

example/ios/Flutter/Flutter.podspec

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#
2-
# NOTE: This podspec is NOT to be published. It is only used as a local source!
3-
# This is a generated file; do not edit or check into version control.
2+
# This podspec is NOT to be published. It is only used as a local source!
3+
# This is a generated file; do not edit or check into version control.
44
#
55

66
Pod::Spec.new do |s|
77
s.name = 'Flutter'
88
s.version = '1.0.0'
9-
s.summary = 'High-performance, high-fidelity mobile apps.'
10-
s.homepage = 'https://flutter.io'
11-
s.license = { :type => 'MIT' }
9+
s.summary = 'A UI toolkit for beautiful and fast apps.'
10+
s.homepage = 'https://flutter.dev'
11+
s.license = { :type => 'BSD' }
1212
s.author = { 'Flutter Dev Team' => '[email protected]' }
1313
s.source = { :git => 'https://github.com/flutter/engine', :tag => s.version.to_s }
14-
s.ios.deployment_target = '8.0'
14+
s.ios.deployment_target = '11.0'
1515
# Framework linking is handled by Flutter tooling, not CocoaPods.
1616
# Add a placeholder to satisfy `s.dependency 'Flutter'` plugin podspecs.
1717
s.vendored_frameworks = 'path/to/nothing'

example/ios/Podfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Uncomment this line to define a global platform for your project
2-
# platform :ios, '9.0'
2+
# platform :ios, '11.0'
33

44
# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
55
ENV['COCOAPODS_DISABLE_STATS'] = 'true'

example/ios/Podfile.lock

+13-26
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,34 @@
11
PODS:
2-
- connectivity (0.0.1):
3-
- Flutter
4-
- Reachability
5-
- device_info (0.0.1):
2+
- device_info_plus (0.0.1):
63
- Flutter
74
- Flutter (1.0.0)
85
- flutter_deriv_api (0.0.1):
96
- Flutter
10-
- package_info (0.0.1):
7+
- package_info_plus (0.4.5):
118
- Flutter
12-
- Reachability (3.2)
139

1410
DEPENDENCIES:
15-
- connectivity (from `.symlinks/plugins/connectivity/ios`)
16-
- device_info (from `.symlinks/plugins/device_info/ios`)
11+
- device_info_plus (from `.symlinks/plugins/device_info_plus/ios`)
1712
- Flutter (from `Flutter`)
1813
- flutter_deriv_api (from `.symlinks/plugins/flutter_deriv_api/ios`)
19-
- package_info (from `.symlinks/plugins/package_info/ios`)
20-
21-
SPEC REPOS:
22-
trunk:
23-
- Reachability
14+
- package_info_plus (from `.symlinks/plugins/package_info_plus/ios`)
2415

2516
EXTERNAL SOURCES:
26-
connectivity:
27-
:path: ".symlinks/plugins/connectivity/ios"
28-
device_info:
29-
:path: ".symlinks/plugins/device_info/ios"
17+
device_info_plus:
18+
:path: ".symlinks/plugins/device_info_plus/ios"
3019
Flutter:
3120
:path: Flutter
3221
flutter_deriv_api:
3322
:path: ".symlinks/plugins/flutter_deriv_api/ios"
34-
package_info:
35-
:path: ".symlinks/plugins/package_info/ios"
23+
package_info_plus:
24+
:path: ".symlinks/plugins/package_info_plus/ios"
3625

3726
SPEC CHECKSUMS:
38-
connectivity: c4130b2985d4ef6fd26f9702e886bd5260681467
39-
device_info: d7d233b645a32c40dfdc212de5cf646ca482f175
40-
Flutter: 434fef37c0980e73bb6479ef766c45957d4b510c
27+
device_info_plus: e5c5da33f982a436e103237c0c85f9031142abed
28+
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
4129
flutter_deriv_api: 9e29abd7cc5091b72303f9c8be549618415f1437
42-
package_info: 873975fc26034f0b863a300ad47e7f1ac6c7ec62
43-
Reachability: 33e18b67625424e47b6cde6d202dce689ad7af96
30+
package_info_plus: 6c92f08e1f853dc01228d6f553146438dafcd14e
4431

45-
PODFILE CHECKSUM: aafe91acc616949ddb318b77800a7f51bffa2a4c
32+
PODFILE CHECKSUM: ef19549a9bc3046e7bb7d2fab4d021637c0c58a3
4633

47-
COCOAPODS: 1.10.1
34+
COCOAPODS: 1.12.1

example/ios/Runner.xcodeproj/project.pbxproj

+24-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
archiveVersion = 1;
44
classes = {
55
};
6-
objectVersion = 46;
6+
objectVersion = 54;
77
objects = {
88

99
/* Begin PBXBuildFile section */
@@ -164,7 +164,7 @@
164164
97C146E61CF9000F007C117D /* Project object */ = {
165165
isa = PBXProject;
166166
attributes = {
167-
LastUpgradeCheck = 1020;
167+
LastUpgradeCheck = 1300;
168168
ORGANIZATIONNAME = "The Chromium Authors";
169169
TargetAttributes = {
170170
97C146ED1CF9000F007C117D = {
@@ -213,19 +213,15 @@
213213
);
214214
inputPaths = (
215215
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh",
216-
"${BUILT_PRODUCTS_DIR}/Reachability/Reachability.framework",
217-
"${BUILT_PRODUCTS_DIR}/connectivity/connectivity.framework",
218-
"${BUILT_PRODUCTS_DIR}/device_info/device_info.framework",
216+
"${BUILT_PRODUCTS_DIR}/device_info_plus/device_info_plus.framework",
219217
"${BUILT_PRODUCTS_DIR}/flutter_deriv_api/flutter_deriv_api.framework",
220-
"${BUILT_PRODUCTS_DIR}/package_info/package_info.framework",
218+
"${BUILT_PRODUCTS_DIR}/package_info_plus/package_info_plus.framework",
221219
);
222220
name = "[CP] Embed Pods Frameworks";
223221
outputPaths = (
224-
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Reachability.framework",
225-
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/connectivity.framework",
226-
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/device_info.framework",
222+
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/device_info_plus.framework",
227223
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/flutter_deriv_api.framework",
228-
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/package_info.framework",
224+
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/package_info_plus.framework",
229225
);
230226
runOnlyForDeploymentPostprocessing = 0;
231227
shellPath = /bin/sh;
@@ -234,10 +230,12 @@
234230
};
235231
3B06AD1E1E4923F5004D2608 /* Thin Binary */ = {
236232
isa = PBXShellScriptBuildPhase;
233+
alwaysOutOfDate = 1;
237234
buildActionMask = 2147483647;
238235
files = (
239236
);
240237
inputPaths = (
238+
"${TARGET_BUILD_DIR}/${INFOPLIST_PATH}",
241239
);
242240
name = "Thin Binary";
243241
outputPaths = (
@@ -248,6 +246,7 @@
248246
};
249247
9740EEB61CF901F6004384FC /* Run Script */ = {
250248
isa = PBXShellScriptBuildPhase;
249+
alwaysOutOfDate = 1;
251250
buildActionMask = 2147483647;
252251
files = (
253252
);
@@ -357,7 +356,7 @@
357356
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
358357
GCC_WARN_UNUSED_FUNCTION = YES;
359358
GCC_WARN_UNUSED_VARIABLE = YES;
360-
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
359+
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
361360
MTL_ENABLE_DEBUG_INFO = NO;
362361
SDKROOT = iphoneos;
363362
SUPPORTED_PLATFORMS = iphoneos;
@@ -379,7 +378,10 @@
379378
"$(PROJECT_DIR)/Flutter",
380379
);
381380
INFOPLIST_FILE = Runner/Info.plist;
382-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
381+
LD_RUNPATH_SEARCH_PATHS = (
382+
"$(inherited)",
383+
"@executable_path/Frameworks",
384+
);
383385
LIBRARY_SEARCH_PATHS = (
384386
"$(inherited)",
385387
"$(PROJECT_DIR)/Flutter",
@@ -439,7 +441,7 @@
439441
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
440442
GCC_WARN_UNUSED_FUNCTION = YES;
441443
GCC_WARN_UNUSED_VARIABLE = YES;
442-
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
444+
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
443445
MTL_ENABLE_DEBUG_INFO = YES;
444446
ONLY_ACTIVE_ARCH = YES;
445447
SDKROOT = iphoneos;
@@ -488,7 +490,7 @@
488490
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
489491
GCC_WARN_UNUSED_FUNCTION = YES;
490492
GCC_WARN_UNUSED_VARIABLE = YES;
491-
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
493+
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
492494
MTL_ENABLE_DEBUG_INFO = NO;
493495
SDKROOT = iphoneos;
494496
SUPPORTED_PLATFORMS = iphoneos;
@@ -511,7 +513,10 @@
511513
"$(PROJECT_DIR)/Flutter",
512514
);
513515
INFOPLIST_FILE = Runner/Info.plist;
514-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
516+
LD_RUNPATH_SEARCH_PATHS = (
517+
"$(inherited)",
518+
"@executable_path/Frameworks",
519+
);
515520
LIBRARY_SEARCH_PATHS = (
516521
"$(inherited)",
517522
"$(PROJECT_DIR)/Flutter",
@@ -538,7 +543,10 @@
538543
"$(PROJECT_DIR)/Flutter",
539544
);
540545
INFOPLIST_FILE = Runner/Info.plist;
541-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
546+
LD_RUNPATH_SEARCH_PATHS = (
547+
"$(inherited)",
548+
"@executable_path/Frameworks",
549+
);
542550
LIBRARY_SEARCH_PATHS = (
543551
"$(inherited)",
544552
"$(PROJECT_DIR)/Flutter",

example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1020"
3+
LastUpgradeVersion = "1300"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

example/ios/Runner/Info.plist

+4
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,9 @@
4141
</array>
4242
<key>UIViewControllerBasedStatusBarAppearance</key>
4343
<false/>
44+
<key>CADisableMinimumFrameDurationOnPhone</key>
45+
<true/>
46+
<key>UIApplicationSupportsIndirectInputEvents</key>
47+
<true/>
4448
</dict>
4549
</plist>

lib/services/connection/api_manager/base_api.dart

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:flutter_deriv_api/basic_api/generated/forget_receive.dart';
44
import 'package:flutter_deriv_api/basic_api/request.dart';
55
import 'package:flutter_deriv_api/basic_api/response.dart';
66
import 'package:flutter_deriv_api/services/connection/api_manager/connection_information.dart';
7+
import 'package:flutter_deriv_api/services/connection/api_manager/enums.dart';
78
import 'package:flutter_deriv_api/services/connection/call_manager/base_call_manager.dart';
89

910
/// Callbacks for websocket connection.
@@ -50,4 +51,10 @@ abstract class BaseAPI {
5051

5152
/// Disconnects from API.
5253
Future<void> disconnect();
54+
55+
/// Gets current websocket connection status.
56+
APIStatus get currentConnectionStatus;
57+
58+
/// Gets websocket connection status.
59+
Stream<APIStatus> get connectionStatus;
5360
}

lib/services/connection/api_manager/binary_api.dart

+31-20
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import 'dart:developer' as dev;
44
import 'dart:io';
55

66
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;
89

910
import 'package:flutter_deriv_api/api/models/enums.dart';
1011
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';
1415
import 'package:flutter_deriv_api/helpers/helpers.dart';
1516
import 'package:flutter_deriv_api/services/connection/api_manager/base_api.dart';
1617
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';
1720
import 'package:flutter_deriv_api/services/connection/call_manager/base_call_manager.dart';
1821
import 'package:flutter_deriv_api/services/connection/call_manager/call_history.dart';
1922
import '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(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// ignore_for_file: public_member_api_docs
2+
3+
enum APIStatus {
4+
connecting,
5+
connected,
6+
disconnected,
7+
}

0 commit comments

Comments
 (0)