Skip to content

Commit

Permalink
Update socket state accordingly and reason about at the beginning of …
Browse files Browse the repository at this point in the history
…_connect()
  • Loading branch information
miguelcmedeiros committed Apr 24, 2024
1 parent 7a94767 commit be10e08
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/src/socket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,12 @@ class PhoenixSocket {

void _connect(Completer<PhoenixSocket?> completer) async {
if (_ws != null &&
(_socketState != SocketState.connected ||
_socketState != SocketState.connecting)) {
(_socketState == SocketState.connected ||
_socketState == SocketState.connecting)) {
_logger.warning(
'Calling connect() on already connected or connecting socket.');
completer.complete(this);
return;
}

_shouldReconnect = true;
Expand All @@ -193,11 +194,6 @@ class PhoenixSocket {
? _webSocketChannelFactory!(_mountPoint)
: WebSocketChannel.connect(_mountPoint);

// Wait for the WebSocket to be ready before continuing. In case of a
// failure to connect, the future will complete with an error and will be
// caught.
await _ws!.ready;

_ws!.stream
.where(_shouldPipeMessage)
.listen(_onSocketData, cancelOnError: true)
Expand All @@ -211,7 +207,13 @@ class PhoenixSocket {
_socketState = SocketState.connecting;

try {
// Wait for the WebSocket to be ready before continuing. In case of a
// failure to connect, the future will complete with an error and will be
// caught.
await _ws!.ready;

_socketState = SocketState.connected;

_logger.finest('Waiting for initial heartbeat roundtrip');
if (await _sendHeartbeat(ignorePreviousHeartbeat: true)) {
_stateStreamController.add(PhoenixSocketOpenEvent());
Expand Down

0 comments on commit be10e08

Please sign in to comment.