Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kpsroka committed Aug 14, 2024
1 parent 1b934cd commit 0e9637f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
15 changes: 8 additions & 7 deletions lib/src/socket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,11 @@ class PhoenixSocket {
if (reconnect) {
_reconnect(code ?? normalClosure, reason: reason);
} else {
_closeConnection(code ?? goingAway, reason: reason);
_closeConnection(
// Should be goingAway, but https://github.com/dart-lang/http/issues/1294
code ?? normalClosure,
reason: reason,
);
}
}

Expand Down Expand Up @@ -407,7 +411,7 @@ class PhoenixSocket {
return false;
} catch (error, stackTrace) {
_logger.warning('Heartbeat message failed', error, stackTrace);
_reconnect(4001, reason: 'Heartbeat timeout');
_reconnect(heartbeatTimedOut, reason: 'Heartbeat timed out');
return false;
}
}
Expand Down Expand Up @@ -485,11 +489,8 @@ class PhoenixSocket {
completer.complete(message);
}

if (message.ref != _latestHeartbeatRef) {
// The connection is alive, prevent heartbeat timeout from closing
// connection.
_latestHeartbeatRef = null;
}
// The connection is alive, prevent heartbeat timeout from closing it.
_latestHeartbeatRef = null;
}

if (message.topic != null && message.topic!.isNotEmpty) {
Expand Down
24 changes: 19 additions & 5 deletions lib/src/socket_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ typedef WebSocketChannelFactory = Future<WebSocketChannel> Function();

final _logger = Logger('phoenix_socket.connection');

// Some custom close codes.
const unknownReason = 4000;
const heartbeatTimedOut = 4001;
const forcedReconnectionRequested = 4002;

/// Maintains connection to the underlying websocket, reconnecting to it if
/// necessary.
class SocketConnectionManager {
Expand Down Expand Up @@ -65,7 +70,8 @@ class SocketConnectionManager {
return;
}

_stopConnecting(4002, 'Immediate connection requested');
_stopConnecting(
forcedReconnectionRequested, 'Immediate connection requested');
_connectionAttempts = 0;
}
_maybeConnect();
Expand Down Expand Up @@ -167,7 +173,8 @@ class SocketConnectionManager {
} finally {
if (_disposed) {
// Manager was disposed while running connection attempt.
connection?.close(goingAway, 'Client disposed');
// Should be goingAway, but https://github.com/dart-lang/http/issues/1294
connection?.close(normalClosure, 'Client disposed');
connectionCompleter.completeError(StateError('Client disposed'));
} else if (connectionFuture != _pendingConnection) {
connection?.close(normalClosure, 'Closing obsolete connection');
Expand All @@ -176,7 +183,7 @@ class SocketConnectionManager {
connectionCompleter
.completeError(StateError('Connection attempt aborted'));
} else {
// _startConnecting() was called during connection attempt, return the
// _connect() was called during connection attempt, return the
// new Future instead.
connectionCompleter.complete(_pendingConnection);
}
Expand Down Expand Up @@ -230,7 +237,11 @@ class SocketConnectionManager {
_onError(error, stackTrace);
}

rethrow;
if (error is ConnectionInitializationException) {
rethrow;
} else {
throw ConnectionInitializationException(error, stackTrace);
}
}
}

Expand Down Expand Up @@ -383,7 +394,10 @@ class _WebSocketConnection {
onError: onError,
onDone: () {
onStateChange(
WebSocketDisconnected._(_ws.closeCode ?? 4000, _ws.closeReason),
WebSocketDisconnected._(
_ws.closeCode ?? unknownReason,
_ws.closeReason,
),
);
acceptingMessages = false;
subscription.cancel();
Expand Down

0 comments on commit 0e9637f

Please sign in to comment.