Skip to content

Commit

Permalink
Merge pull request #112 from achilleasa/fix-reconnect-related-bugs
Browse files Browse the repository at this point in the history
Fix reconnect related bugs
  • Loading branch information
achilleasa authored Jan 4, 2024
2 parents adae0c2 + 932f748 commit 6cdc538
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/src/client/impl/client_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class _ClientImpl implements Client {
// period has been configured, start monitoring incoming heartbeats.
if (serverMessage.message is ConnectionOpenOk &&
tuningSettings.heartbeatPeriod.inSeconds > 0) {
_heartbeatRecvTimer?.cancel();
_heartbeatRecvTimer =
RestartableTimer(tuningSettings.heartbeatPeriod, () {
// Set the timer to null to avoid accidentally resetting it while
Expand Down Expand Up @@ -214,7 +215,7 @@ class _ClientImpl implements Client {
if (handshaking) {
_channels.clear();
_connected!.completeError(ex);
close();
_close();
return;
}

Expand All @@ -233,7 +234,7 @@ class _ClientImpl implements Client {
.reversed
.forEach((_ChannelImpl channel) => channel.handleException(ex));

close();
_close();
break;
case ChannelException:
// Forward to the appropriate channel and remove it from our list
Expand Down Expand Up @@ -265,6 +266,13 @@ class _ClientImpl implements Client {
/// when the client has shut down
@override
Future close() {
return _close(closeErrorStream: true);
}

Future _close({bool closeErrorStream = false}) {
_heartbeatRecvTimer?.cancel();
_heartbeatRecvTimer = null;

if (_socket == null) {
return Future.value();
}
Expand All @@ -287,7 +295,9 @@ class _ClientImpl implements Client {
_socket!.destroy();
_socket = null;
_connected = null;
_error.close();
if (closeErrorStream) {
_error.close();
}
_clientClosed!.complete();
_clientClosed = null;
});
Expand Down

0 comments on commit 6cdc538

Please sign in to comment.