improved ios impl

This commit is contained in:
mertalev
2026-02-11 17:29:34 -05:00
parent 60a57ec99d
commit c99e833435
3 changed files with 11 additions and 32 deletions

View File

@@ -9,24 +9,26 @@ import 'package:web_socket/web_socket.dart';
class NetworkRepository {
static http.Client? _client;
static late int _clientPointer;
static late Pointer<Void> _clientPointer;
static Future<void> init() async {
_clientPointer = await networkApi.getClientPointer();
_clientPointer = Pointer<Void>.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<WebSocket> createWebSocket(Uri uri, {Map<String, String>? headers, Iterable<String>? 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<Void>.fromAddress(address);
return OkHttpClient.fromJniGlobalRef(pointer);
}
static Future<WebSocket> _createIOSWebSocket(Uri uri, {Iterable<String>? 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<WebSocket> _createAndroidWebSocket(Uri uri, {Iterable<String>? protocols}) {
final pointer = Pointer<Void>.fromAddress(_clientPointer);
return OkHttpWebSocket.connectFromJniGlobalRef(pointer, uri, protocols: protocols);
}
}

View File

@@ -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"

View File

@@ -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: