-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Await for a successful connection to the websocket before listening to messages #76
Await for a successful connection to the websocket before listening to messages #76
Conversation
@matehat the integration sockets tests are failing. I will investigate what's happening there and let you know if I need help with them. |
@@ -508,10 +516,6 @@ class PhoenixSocket { | |||
void _onSocketData(message) => onSocketDataCallback(message); | |||
|
|||
void _onSocketError(dynamic error, dynamic stacktrace) { | |||
if (_socketState == SocketState.closing || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@matehat
While testing the original change, I noticed that this check was preventing the socket from being reconnected, since _onSocketClosed()
is called after it. The scheduling of the socket reconnection happens at that point.
Does it make sense?
lib/src/socket.dart
Outdated
@@ -171,7 +171,9 @@ class PhoenixSocket { | |||
bool get isConnected => _ws != null && _socketState == SocketState.connected; | |||
|
|||
void _connect(Completer<PhoenixSocket?> completer) async { | |||
if (_ws != null) { | |||
if (_ws != null && | |||
(_socketState != SocketState.connected || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@matehat
In the case of a reconnection, we have a closed socket, then we would not attempt to reconnect.
If the socket state is unknown, closing or closed, then we are able to proceed with the reconnection of the socket.
49c79f9
to
c964569
Compare
c964569
to
be10e08
Compare
Great investigation! Thanks @miguelcmedeiros for your work :) |
…o messages (braverhealth#76) * Await for a successful connection to the websocket before listening to messages * Fix socket reconnection * Update socket state accordingly and reason about at the beginning of _connect()
Similar to issues (issue 1 and issue 2) reported in web_socket_channel package.
When the websocket connection is created and there's an exception thrown at this point we won't be able to catch it, leaving the client in an unrecoverable state (i.e. since the exception is not caught, the websocket artefacts are not properly cleared up).
socket.ready
will complete with an error in case something goes wrong duringWebSocketChannel.connect()
.It's not an obvious API, but it seems to be the way to go with this package.