Skip to content

Commit

Permalink
upgrade to connectivity plus 6.x. (#503)
Browse files Browse the repository at this point in the history
* upgrade to connectivity plus 6.x.

* fix.

* fix.
  • Loading branch information
cloudwebrtc authored Apr 19, 2024
1 parent eb11920 commit c44f861
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 36 deletions.
2 changes: 1 addition & 1 deletion lib/src/core/engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
logger.fine('no internet connection, waiting...');
await signalClient.events.waitFor<SignalConnectivityChangedEvent>(
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'),
);
Expand Down
21 changes: 10 additions & 11 deletions lib/src/core/signal_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,17 @@ class SignalClient extends Disposable with EventsEmittable<SignalEvent> {
int _pingCount = 0;
String? participantSid;

ConnectivityResult? _connectivityResult;
StreamSubscription<ConnectivityResult>? connectivitySubscription;
List<ConnectivityResult> _connectivityResult = [];
StreamSubscription<List<ConnectivityResult>>? connectivitySubscription;

Future<bool> networkIsAvailable() async {
// Skip check for web or flutter test
if (kIsWeb || lkPlatformIsTest()) {
return true;
}
_connectivityResult = await Connectivity().checkConnectivity();
return _connectivityResult != ConnectivityResult.none;
return _connectivityResult.isNotEmpty &&
!_connectivityResult.contains(ConnectivityResult.none);
}

@internal
Expand Down Expand Up @@ -95,27 +96,25 @@ class SignalClient extends Disposable with EventsEmittable<SignalEvent> {
}) async {
if (!kIsWeb && !lkPlatformIsTest()) {
_connectivityResult = await Connectivity().checkConnectivity();
connectivitySubscription ??= Connectivity()
connectivitySubscription = Connectivity()
.onConnectivityChanged
.listen((ConnectivityResult result) {
.listen((List<ConnectivityResult> 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));
Expand Down
4 changes: 2 additions & 2 deletions lib/src/internal/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ class SignalReconnectResponseEvent with SignalEvent, InternalEvent {

@internal
class SignalConnectivityChangedEvent with SignalEvent, InternalEvent {
final ConnectivityResult oldState;
final ConnectivityResult state;
final List<ConnectivityResult> oldState;
final List<ConnectivityResult> state;
const SignalConnectivityChangedEvent({
required this.oldState,
required this.state,
Expand Down
35 changes: 14 additions & 21 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c44f861

Please sign in to comment.