diff --git a/mobile/lib/infrastructure/repositories/network.repository.dart b/mobile/lib/infrastructure/repositories/network.repository.dart index 9bf57b9120..e99e30e19f 100644 --- a/mobile/lib/infrastructure/repositories/network.repository.dart +++ b/mobile/lib/infrastructure/repositories/network.repository.dart @@ -9,24 +9,26 @@ import 'package:web_socket/web_socket.dart'; class NetworkRepository { static http.Client? _client; - static late int _clientPointer; + static late Pointer _clientPointer; static Future init() async { - _clientPointer = await networkApi.getClientPointer(); + _clientPointer = Pointer.fromAddress(await networkApi.getClientPointer()); _client?.close(); if (Platform.isIOS) { - _client = _createIOSClient(_clientPointer); + final session = URLSession.fromRawPointer(_clientPointer.cast()); + _client = CupertinoClient.fromSharedSession(session); } else { - _client = _createAndroidClient(_clientPointer); + _client = OkHttpClient.fromJniGlobalRef(_clientPointer); } } // ignore: avoid-unused-parameters static Future createWebSocket(Uri uri, {Map? headers, Iterable? protocols}) { if (Platform.isIOS) { - return _createIOSWebSocket(uri, protocols: protocols); + final session = URLSession.fromRawPointer(_clientPointer.cast()); + return CupertinoWebSocket.connectWithSession(session, uri, protocols: protocols); } else { - return _createAndroidWebSocket(uri, protocols: protocols); + return OkHttpWebSocket.connectFromJniGlobalRef(_clientPointer, uri, protocols: protocols); } } @@ -39,27 +41,4 @@ class NetworkRepository { /// /// Must call [init] before using this method. static http.Client get client => _client!; - - static http.Client _createIOSClient(int address) { - final pointer = Pointer.fromAddress(address); - final session = URLSession.fromRawPointer(pointer.cast()); - return CupertinoClient.fromSharedSession(session); - } - - static http.Client _createAndroidClient(int address) { - final pointer = Pointer.fromAddress(address); - return OkHttpClient.fromJniGlobalRef(pointer); - } - - static Future _createIOSWebSocket(Uri uri, {Iterable? protocols}) async { - final result = await networkApi.createWebSocketTask(uri.toString(), protocols?.toList()); - final pointer = Pointer.fromAddress(result.taskPointer); - final task = URLSessionWebSocketTask.fromRawPointer(pointer.cast()); - return CupertinoWebSocket.fromConnectedTask(task, protocol: result.taskProtocol ?? ''); - } - - static Future _createAndroidWebSocket(Uri uri, {Iterable? protocols}) { - final pointer = Pointer.fromAddress(_clientPointer); - return OkHttpWebSocket.connectFromJniGlobalRef(pointer, uri, protocols: protocols); - } } diff --git a/mobile/pubspec.lock b/mobile/pubspec.lock index d4500c0d3a..f1a60e6701 100644 --- a/mobile/pubspec.lock +++ b/mobile/pubspec.lock @@ -333,8 +333,8 @@ packages: dependency: "direct main" description: path: "pkgs/cupertino_http" - ref: "6a28337a5d759bee3d198992e79d9b5c1e80fd3a" - resolved-ref: "6a28337a5d759bee3d198992e79d9b5c1e80fd3a" + ref: a0a933358517c6d01cff37fc2a2752ee2d744a3c + resolved-ref: a0a933358517c6d01cff37fc2a2752ee2d744a3c url: "https://github.com/mertalev/http" source: git version: "3.0.0-wip" diff --git a/mobile/pubspec.yaml b/mobile/pubspec.yaml index a324f0be59..26a7d25cd2 100644 --- a/mobile/pubspec.yaml +++ b/mobile/pubspec.yaml @@ -90,7 +90,7 @@ dependencies: cupertino_http: git: url: https://github.com/mertalev/http - ref: '6a28337a5d759bee3d198992e79d9b5c1e80fd3a' # https://github.com/dart-lang/http/pull/1876 + ref: 'a0a933358517c6d01cff37fc2a2752ee2d744a3c' # https://github.com/dart-lang/http/pull/1876 path: pkgs/cupertino_http/ ok_http: git: