Skip to content

Commit

Permalink
Improve reconnection events, add RoomAttemptReconnectEvent.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc committed Dec 22, 2023
1 parent b1c5751 commit 1379d0b
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 18 deletions.
5 changes: 5 additions & 0 deletions example/lib/pages/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ class _RoomPageState extends State<RoomPage> {
..on<RoomRecordingStatusChanged>((event) {
context.showRecordingStatusChangedDialog(event.activeRecording);
})
..on<RoomAttemptReconnectEvent>((event) {
print(
'Attempting to reconnect ${event.attempt}/${event.maxAttemptsRetry}, '
'(${event.nextRetryDelaysInMs}ms delay until next attempt)');
})
..on<LocalTrackPublishedEvent>((_) => _sortParticipants())
..on<LocalTrackUnpublishedEvent>((_) => _sortParticipants())
..on<TrackSubscribedEvent>((_) => _sortParticipants())
Expand Down
33 changes: 21 additions & 12 deletions lib/src/core/engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
await signalClient.cleanUp();

fullReconnectOnNext = false;
attemptingReconnect = false;

clearPendingReconnect();
}
Expand Down Expand Up @@ -625,8 +626,25 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
reconnectStart = DateTime.now();
}

if (reconnectAttempts! >= _reconnectCount) {
logger.fine('reconnectAttempts exceeded, disconnecting...');
_isClosed = true;
await cleanUp();

events.emit(EngineDisconnectedEvent(
reason: DisconnectReason.reconnectAttemptsExceeded,
));
return;
}

var delay = defaultRetryDelaysInMs[reconnectAttempts!];

events.emit(EngineAttemptReconnectEvent(
attempt: reconnectAttempts! + 1,
maxAttempts: _reconnectCount,
nextRetryDelaysInMs: delay,
));

clearReconnectTimeout();
logger.fine(
'WebSocket reconnecting in $delay ms, retry times $reconnectAttempts');
Expand Down Expand Up @@ -656,15 +674,6 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
fullReconnectOnNext = true;
}

if (reconnectAttempts! >= _reconnectCount) {
logger.fine('reconnectAttempts exceeded, disconnecting...');
events.emit(EngineDisconnectedEvent(
reason: DisconnectReason.connectionClosed,
));
await cleanUp();
return;
}

try {
attemptingReconnect = true;

Expand Down Expand Up @@ -704,7 +713,7 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
} else {
logger.fine('attemptReconnect: disconnecting...');
events.emit(EngineDisconnectedEvent(
reason: DisconnectReason.connectionClosed,
reason: DisconnectReason.disconnected,
));
await cleanUp();
}
Expand All @@ -718,7 +727,7 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
return;
}

events.emit(const EngineResumingEvent());
events.emit(const EngineReconnectingEvent());

// wait for socket to connect rtc server
await signalClient.connect(
Expand Down Expand Up @@ -915,7 +924,7 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
})
..on<SignalDisconnectedEvent>((event) async {
logger.fine('Signal disconnected ${event.reason}');
if (event.reason == DisconnectReason.connectionClosed && !_isClosed) {
if (event.reason == DisconnectReason.disconnected && !_isClosed) {
await handleDisconnect(ClientDisconnectReason.signal);
} else {
events.emit(EngineDisconnectedEvent(
Expand Down
22 changes: 19 additions & 3 deletions lib/src/core/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

import 'dart:async';
import 'dart:math';

import 'package:flutter/foundation.dart';

Expand Down Expand Up @@ -408,11 +409,26 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
await _sendSyncState();
notifyListeners();
})
..on<EngineDisconnectedEvent>((event) async {
await _cleanUp();
events.emit(RoomDisconnectedEvent(reason: event.reason));
..on<EngineAttemptReconnectEvent>((event) async {
events.emit(RoomAttemptReconnectEvent(
attempt: event.attempt,
maxAttemptsRetry: event.maxAttempts,
nextRetryDelaysInMs: event.nextRetryDelaysInMs,
));
notifyListeners();
})
..on<EngineDisconnectedEvent>((event) async {
if (!engine.fullReconnectOnNext &&
![
DisconnectReason.signalingConnectionFailure,
DisconnectReason.joinFailure,
DisconnectReason.noInternetConnection
].contains(event.reason)) {
await _cleanUp();
events.emit(RoomDisconnectedEvent(reason: event.reason));
notifyListeners();
}
})
..on<EngineActiveSpeakersUpdateEvent>(
(event) => _onEngineActiveSpeakersUpdateEvent(event.speakers))
..on<EngineDataPacketReceivedEvent>(_onDataMessageEvent)
Expand Down
3 changes: 1 addition & 2 deletions lib/src/core/signal_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,7 @@ class SignalClient extends Disposable with EventsEmittable<SignalEvent> {
return;
}
_connectionState = ConnectionState.disconnected;
events.emit(
SignalDisconnectedEvent(reason: DisconnectReason.connectionClosed));
events.emit(SignalDisconnectedEvent(reason: DisconnectReason.disconnected));
}

void _sendPing() {
Expand Down
15 changes: 15 additions & 0 deletions lib/src/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ class RoomReconnectingEvent with RoomEvent {
String toString() => '${runtimeType}()';
}

/// report the number of attempts to reconnect to the room.
class RoomAttemptReconnectEvent with RoomEvent {
final int attempt;
final int maxAttemptsRetry;
final int nextRetryDelaysInMs;
const RoomAttemptReconnectEvent({
required this.attempt,
required this.maxAttemptsRetry,
required this.nextRetryDelaysInMs,
});

@override
String toString() => '${runtimeType}()';
}

/// Connection to room is re-established. All existing state is preserved.
/// Emitted by [Room].
class RoomReconnectedEvent with RoomEvent {
Expand Down
12 changes: 12 additions & 0 deletions lib/src/internal/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ class EngineFullRestartingEvent with InternalEvent, EngineEvent {
const EngineFullRestartingEvent();
}

@internal
class EngineAttemptReconnectEvent with InternalEvent, EngineEvent {
int attempt;
int maxAttempts;
int nextRetryDelaysInMs;
EngineAttemptReconnectEvent({
required this.attempt,
required this.maxAttempts,
required this.nextRetryDelaysInMs,
});
}

@internal
class EngineRestartedEvent with InternalEvent, EngineEvent {
const EngineRestartedEvent();
Expand Down
3 changes: 2 additions & 1 deletion lib/src/types/other.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ enum DisconnectReason {
roomDeleted,
stateMismatch,
joinFailure,
connectionClosed,
disconnected,
signalingConnectionFailure,
noInternetConnection,
reconnectAttemptsExceeded,
}

/// The reason why a track failed to publish.
Expand Down

0 comments on commit 1379d0b

Please sign in to comment.