From 6ce168beb2c8b46d17b5a9c3d76910c02c11dae0 Mon Sep 17 00:00:00 2001 From: Miguel Medeiros <31278849+miguelcmedeiros@users.noreply.github.com> Date: Thu, 18 Apr 2024 09:53:58 +0100 Subject: [PATCH 1/3] Await for a successful connection to the websocket before listening to messages --- lib/src/socket.dart | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/src/socket.dart b/lib/src/socket.dart index e2a6d6d..1b09f45 100644 --- a/lib/src/socket.dart +++ b/lib/src/socket.dart @@ -190,6 +190,12 @@ class PhoenixSocket { _ws = _webSocketChannelFactory != null ? _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) From 7a9476737f02cee269f27aee02868c1da5f603de Mon Sep 17 00:00:00 2001 From: Miguel Medeiros <31278849+miguelcmedeiros@users.noreply.github.com> Date: Fri, 19 Apr 2024 12:07:38 +0100 Subject: [PATCH 2/3] Fix socket reconnection --- lib/src/socket.dart | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/src/socket.dart b/lib/src/socket.dart index 1b09f45..c24088a 100644 --- a/lib/src/socket.dart +++ b/lib/src/socket.dart @@ -171,7 +171,9 @@ class PhoenixSocket { bool get isConnected => _ws != null && _socketState == SocketState.connected; void _connect(Completer completer) async { - if (_ws != null) { + if (_ws != null && + (_socketState != SocketState.connected || + _socketState != SocketState.connecting)) { _logger.warning( 'Calling connect() on already connected or connecting socket.'); completer.complete(this); @@ -514,10 +516,6 @@ class PhoenixSocket { void _onSocketData(message) => onSocketDataCallback(message); void _onSocketError(dynamic error, dynamic stacktrace) { - if (_socketState == SocketState.closing || - _socketState == SocketState.closed) { - return; - } final socketError = PhoenixSocketErrorEvent( error: error, stacktrace: stacktrace, From be10e0820202713e111cca0d816a6e0e8a17f757 Mon Sep 17 00:00:00 2001 From: Miguel Medeiros <31278849+miguelcmedeiros@users.noreply.github.com> Date: Tue, 23 Apr 2024 18:19:29 +0100 Subject: [PATCH 3/3] Update socket state accordingly and reason about at the beginning of _connect() --- lib/src/socket.dart | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/src/socket.dart b/lib/src/socket.dart index c24088a..f2adf64 100644 --- a/lib/src/socket.dart +++ b/lib/src/socket.dart @@ -172,11 +172,12 @@ class PhoenixSocket { void _connect(Completer 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; @@ -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) @@ -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());