Skip to content

Commit

Permalink
fix reconnect test.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc committed Dec 18, 2023
1 parent 121a95d commit 242919a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
1 change: 1 addition & 0 deletions lib/src/core/signal_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class SignalClient extends Disposable with EventsEmittable<SignalEvent> {
ConnectionState get connectionState => _connectionState;

final WebSocketUtility _ws;
WebSocketUtility get ws => _ws;

final _queue = Queue<lk_rtc.SignalRequest>();
Duration? _pingTimeoutDuration;
Expand Down
46 changes: 29 additions & 17 deletions lib/src/support/websocket_utility.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@

import 'dart:async';

import 'package:flutter/foundation.dart';

import 'package:connectivity_plus/connectivity_plus.dart';

import '../logger.dart';
import 'platform.dart';
import 'websocket.dart';

enum SocketStatus {
Expand Down Expand Up @@ -75,23 +78,25 @@ class WebSocketUtility {
_socketStatus = SocketStatus.kSocketStatusNone;
onSocketStatusChange?.call(_socketStatus);

if (_connectivitySubscription != null) {
await _connectivitySubscription?.cancel();
_connectivitySubscription = null;
}
if (!lkPlatformIsTest()) {
if (_connectivitySubscription != null) {
await _connectivitySubscription?.cancel();
_connectivitySubscription = null;
}

_connectivity = await Connectivity().checkConnectivity();
_connectivitySubscription =
Connectivity().onConnectivityChanged.listen((result) {
logger.fine('network changed ${result.name}, reconnecting...');
if ((result != ConnectivityResult.none && _connectivity != result) &&
!_isClosed) {
if (_socketStatus != SocketStatus.kSocketStatusNone) {
reconnect();
_connectivity = await Connectivity().checkConnectivity();
_connectivitySubscription =
Connectivity().onConnectivityChanged.listen((result) {
logger.fine('network changed ${result.name}, reconnecting...');
if ((result != ConnectivityResult.none && _connectivity != result) &&
!_isClosed) {
if (_socketStatus != SocketStatus.kSocketStatusNone) {
reconnect();
}
}
}
_connectivity = result;
});
_connectivity = result;
});
}
}

void setReconnSid(String sid) {
Expand Down Expand Up @@ -182,8 +187,10 @@ class WebSocketUtility {
}
_reconnectTimes = 0;
_isClosed = true;
_connectivitySubscription?.cancel();
_connectivitySubscription = null;
if (!lkPlatformIsTest()) {
_connectivitySubscription?.cancel();
_connectivitySubscription = null;
}
_cleanUp();
}

Expand All @@ -196,10 +203,15 @@ class WebSocketUtility {
}

Future<bool> reconnect() async {
if (lkPlatformIsTest()) {
_reconnectUrl = Uri.tryParse('ws://example.com');
}

if (_reconnectUrl == null) {
logger.warning('WebSocket reconnect failed, no reconnect url');
return false;
}

if (_reconnectTimes < _reconnectCount) {
if (_socketStatus != SocketStatus.kSocketStatusReconnecting) {
_changeSocketStatus(SocketStatus.kSocketStatusReconnecting);
Expand Down
15 changes: 9 additions & 6 deletions test/core/signal_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,24 @@ void main() {
);
});
test('reconnect', () async {
await client.connect(
exampleUri,
token,
connectOptions: connectOptions,
roomOptions: roomOptions,
);
expect(
client.events.streamCtrl.stream,
emitsInOrder(<Matcher>[
predicate<SignalConnectionStateUpdatedEvent>(
(event) => event.newState == ConnectionState.disconnected),
predicate<SignalConnectionStateUpdatedEvent>(
(event) => event.newState == ConnectionState.reconnecting),
predicate<SignalConnectionStateUpdatedEvent>((event) =>
event.newState == ConnectionState.connected &&
event.didReconnect == true),
]));
await client.connect(
exampleUri,
token,
connectOptions: connectOptions,
roomOptions: roomOptions,
);
await client.closeSocket();
});
});

Expand Down

0 comments on commit 242919a

Please sign in to comment.