From c44f861efa4def83ed037ec0101b206294a34362 Mon Sep 17 00:00:00 2001 From: CloudWebRTC Date: Fri, 19 Apr 2024 16:38:23 +0800 Subject: [PATCH] upgrade to connectivity plus 6.x. (#503) * upgrade to connectivity plus 6.x. * fix. * fix. --- lib/src/core/engine.dart | 2 +- lib/src/core/signal_client.dart | 21 ++++++++++---------- lib/src/internal/events.dart | 4 ++-- lib/src/utils.dart | 35 +++++++++++++-------------------- pubspec.yaml | 2 +- 5 files changed, 28 insertions(+), 36 deletions(-) diff --git a/lib/src/core/engine.dart b/lib/src/core/engine.dart index f51b3175..1c323450 100644 --- a/lib/src/core/engine.dart +++ b/lib/src/core/engine.dart @@ -687,7 +687,7 @@ class Engine extends Disposable with EventsEmittable { logger.fine('no internet connection, waiting...'); await signalClient.events.waitFor( duration: connectOptions.timeouts.connection * 10, - filter: (event) => event.state != ConnectivityResult.none, + filter: (event) => !event.state.contains(ConnectivityResult.none), onTimeout: () => throw ConnectException( 'attemptReconnect: Timed out waiting for SignalConnectivityChangedEvent'), ); diff --git a/lib/src/core/signal_client.dart b/lib/src/core/signal_client.dart index 3d9e67dc..e039069f 100644 --- a/lib/src/core/signal_client.dart +++ b/lib/src/core/signal_client.dart @@ -58,8 +58,8 @@ class SignalClient extends Disposable with EventsEmittable { int _pingCount = 0; String? participantSid; - ConnectivityResult? _connectivityResult; - StreamSubscription? connectivitySubscription; + List _connectivityResult = []; + StreamSubscription>? connectivitySubscription; Future networkIsAvailable() async { // Skip check for web or flutter test @@ -67,7 +67,8 @@ class SignalClient extends Disposable with EventsEmittable { return true; } _connectivityResult = await Connectivity().checkConnectivity(); - return _connectivityResult != ConnectivityResult.none; + return _connectivityResult.isNotEmpty && + !_connectivityResult.contains(ConnectivityResult.none); } @internal @@ -95,27 +96,25 @@ class SignalClient extends Disposable with EventsEmittable { }) async { if (!kIsWeb && !lkPlatformIsTest()) { _connectivityResult = await Connectivity().checkConnectivity(); - connectivitySubscription ??= Connectivity() + connectivitySubscription = Connectivity() .onConnectivityChanged - .listen((ConnectivityResult result) { + .listen((List result) { if (_connectivityResult != result) { - if (result == ConnectivityResult.none) { + if (result.contains(ConnectivityResult.none)) { logger.warning('lost connectivity'); } else { logger.info( - 'Connectivity changed, ${_connectivityResult!.name} => ${result.name}'); + 'Connectivity changed, ${_connectivityResult} => ${result}'); } - events.emit(SignalConnectivityChangedEvent( - oldState: _connectivityResult!, + oldState: _connectivityResult, state: result, )); - _connectivityResult = result; } }); - if (_connectivityResult == ConnectivityResult.none) { + if (_connectivityResult.contains(ConnectivityResult.none)) { logger.warning('no internet connection'); events.emit(SignalDisconnectedEvent( reason: DisconnectReason.noInternetConnection)); diff --git a/lib/src/internal/events.dart b/lib/src/internal/events.dart index 5e7cdb2e..5482c3fa 100644 --- a/lib/src/internal/events.dart +++ b/lib/src/internal/events.dart @@ -143,8 +143,8 @@ class SignalReconnectResponseEvent with SignalEvent, InternalEvent { @internal class SignalConnectivityChangedEvent with SignalEvent, InternalEvent { - final ConnectivityResult oldState; - final ConnectivityResult state; + final List oldState; + final List state; const SignalConnectivityChangedEvent({ required this.oldState, required this.state, diff --git a/lib/src/utils.dart b/lib/src/utils.dart index a13b9027..7295e7b7 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -336,27 +336,20 @@ class Utils { var connectivityResult = await (Connectivity().checkConnectivity()); // wifi, wired, cellular, vpn, empty if not known String networkType = 'empty'; - switch (connectivityResult) { - case ConnectivityResult.mobile: - networkType = 'cellular'; - break; - case ConnectivityResult.wifi: - networkType = 'wifi'; - break; - case ConnectivityResult.bluetooth: - networkType = 'bluetooth'; - break; - case ConnectivityResult.ethernet: - networkType = 'wired'; - break; - case ConnectivityResult.other: - case ConnectivityResult.vpn: - //TODO: will livekit-server handle vpn and other types correctly? - // networkType = 'vpn'; - break; - case ConnectivityResult.none: - networkType = 'empty'; - break; + if (connectivityResult.contains(ConnectivityResult.none)) { + networkType = 'empty'; + } else if (connectivityResult.contains(ConnectivityResult.mobile)) { + networkType = 'cellular'; + } else if (connectivityResult.contains(ConnectivityResult.wifi)) { + networkType = 'wifi'; + } else if (connectivityResult.contains(ConnectivityResult.ethernet)) { + networkType = 'wired'; + } else if (connectivityResult.contains(ConnectivityResult.bluetooth)) { + networkType = 'bluetooth'; + } else if (connectivityResult.contains(ConnectivityResult.other)) { + networkType = 'other'; + } else if (connectivityResult.contains(ConnectivityResult.vpn)) { + networkType = 'vpn'; } return networkType; } diff --git a/pubspec.yaml b/pubspec.yaml index b8b2fe21..4033c21a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -29,7 +29,7 @@ dependencies: sdk: flutter async: ^2.9.0 collection: '>=1.16.0' - connectivity_plus: ^5.0.0 + connectivity_plus: ^6.0.2 cryptography: ^2.0.5 fixnum: ^1.0.1 meta: ^1.8.0