Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changes/add-reconnect-reason
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
patch type="added" "Send reconnect reason to server during WebSocket reconnection"
48 changes: 35 additions & 13 deletions lib/src/core/engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,10 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
if (error is NegotiationError) {
fullReconnectOnNext = true;
}
await handleReconnect(ClientDisconnectReason.negotiationFailed);
await handleReconnect(
ClientDisconnectReason.negotiationFailed,
reconnectReason: lk_models.ReconnectReason.RR_UNKNOWN,
);
}
}

Expand Down Expand Up @@ -669,9 +672,10 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
));
logger.fine('subscriber connectionState: $state');
if (state.isDisconnected() || state.isFailed()) {
await handleReconnect(state.isFailed()
? ClientDisconnectReason.peerConnectionFailed
: ClientDisconnectReason.peerConnectionClosed);
await handleReconnect(
state.isFailed() ? ClientDisconnectReason.peerConnectionFailed : ClientDisconnectReason.peerConnectionClosed,
reconnectReason: lk_models.ReconnectReason.RR_SUBSCRIBER_FAILED,
);
}
};

Expand All @@ -689,9 +693,10 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
));
logger.fine('publisher connectionState: $state');
if (state.isDisconnected() || state.isFailed()) {
await handleReconnect(state.isFailed()
? ClientDisconnectReason.peerConnectionFailed
: ClientDisconnectReason.peerConnectionClosed);
await handleReconnect(
state.isFailed() ? ClientDisconnectReason.peerConnectionFailed : ClientDisconnectReason.peerConnectionClosed,
reconnectReason: lk_models.ReconnectReason.RR_PUBLISHER_FAILED,
);
}
};

Expand Down Expand Up @@ -980,7 +985,10 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
}

@internal
Future<void> handleReconnect(ClientDisconnectReason reason) async {
Future<void> handleReconnect(
ClientDisconnectReason reason, {
lk_models.ReconnectReason? reconnectReason,
}) async {
if (_isClosed) {
logger.fine('handleReconnect: engine is closed, skip');
return;
Expand Down Expand Up @@ -1021,12 +1029,18 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
}
logger.fine('WebSocket reconnecting in $delay ms, retry times $reconnectAttempts');
reconnectTimeout = Timer(Duration(milliseconds: delay), () async {
await attemptReconnect(reason);
await attemptReconnect(
reason,
reconnectReason: reconnectReason,
);
});
}

@internal
Future<void> attemptReconnect(ClientDisconnectReason reason) async {
Future<void> attemptReconnect(
ClientDisconnectReason reason, {
lk_models.ReconnectReason? reconnectReason,
}) async {
if (_isClosed) {
return;
}
Expand Down Expand Up @@ -1062,7 +1076,10 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
if (fullReconnectOnNext) {
await restartConnection();
} else {
await resumeConnection(reason);
await resumeConnection(
reason,
reconnectReason: reconnectReason,
);
}
clearPendingReconnect();
attemptingReconnect = false;
Expand Down Expand Up @@ -1093,7 +1110,10 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
}
}

Future<void> resumeConnection(ClientDisconnectReason reason) async {
Future<void> resumeConnection(
ClientDisconnectReason reason, {
lk_models.ReconnectReason? reconnectReason,
}) async {
if (_isClosed) {
return;
}
Expand All @@ -1107,6 +1127,7 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
connectOptions: connectOptions,
roomOptions: roomOptions,
reconnect: true,
reconnectReason: reconnectReason,
);

await events.waitFor<SignalReconnectedEvent>(
Expand Down Expand Up @@ -1322,7 +1343,8 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
..on<SignalDisconnectedEvent>((event) async {
logger.fine('Signal disconnected ${event.reason}');
if (event.reason == DisconnectReason.disconnected && !_isClosed) {
await handleReconnect(ClientDisconnectReason.signal);
await handleReconnect(ClientDisconnectReason.signal,
reconnectReason: lk_models.ReconnectReason.RR_SIGNAL_DISCONNECTED);
} else if (event.reason == DisconnectReason.signalingConnectionFailure) {
events.emit(EngineDisconnectedEvent(
reason: event.reason,
Expand Down
2 changes: 2 additions & 0 deletions lib/src/core/signal_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class SignalClient extends Disposable with EventsEmittable<SignalEvent> {
required ConnectOptions connectOptions,
required RoomOptions roomOptions,
bool reconnect = false,
lk_models.ReconnectReason? reconnectReason,
}) async {
if (!kIsWeb && !lkPlatformIsTest()) {
_connectivityResult = await Connectivity().checkConnectivity();
Expand Down Expand Up @@ -133,6 +134,7 @@ class SignalClient extends Disposable with EventsEmittable<SignalEvent> {
roomOptions: roomOptions,
reconnect: reconnect,
sid: reconnect ? participantSid : null,
reconnectReason: reconnectReason,
);

logger.fine('SignalClient connecting with url: $rtcUri');
Expand Down
2 changes: 2 additions & 0 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ class Utils {
bool validate = false,
bool forceSecure = false,
String? sid,
lk_models.ReconnectReason? reconnectReason,
}) async {
final Uri uri = Uri.parse(uriString);

Expand All @@ -191,6 +192,7 @@ class Utils {
'auto_subscribe': connectOptions.autoSubscribe ? '1' : '0',
'adaptive_stream': roomOptions.adaptiveStream ? '1' : '0',
if (reconnect) 'reconnect': '1',
if (reconnect && reconnectReason != null) 'reconnect_reason': reconnectReason.value.toString(),
if (reconnect && sid != null) 'sid': sid,
'protocol': connectOptions.protocolVersion.toStringValue(),
'sdk': 'flutter',
Expand Down