Skip to content
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

[gql_websocket_link] enhancement for websocket connection errors #412

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
13 changes: 13 additions & 0 deletions links/gql_websocket_link/lib/src/link.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ class WebSocketLink extends Link {
Stream<ConnectionState> get connectionStateStream =>
_connectionStateController.stream;

/// A stream that notifies about connection errors.
final StreamController<ConnectionError> _connectionErrorController =
StreamController<ConnectionError>.broadcast();
Stream<ConnectionError> get connectionErrorStream =>
_connectionErrorController.stream;

/// Initialize the [WebSocketLink] with a [uri].
/// You can customize the headers & protocols by passing [channelGenerator],
/// if [channelGenerator] is passed, [uri] must be null.
Expand Down Expand Up @@ -245,6 +251,8 @@ class WebSocketLink extends Link {
).catchError(_messagesController.addError);
});
_reConnectRequests.clear();
} else if (parsedMessage is ConnectionError) {
_connectionErrorController.add(parsedMessage);
}
}, onDone: () {
if (isDisabled) {
Expand All @@ -266,6 +274,10 @@ class WebSocketLink extends Link {
_close();
}
}, onError: (Object error) {
if (autoReconnect && error is WebSocketChannelException) {
_connectionErrorController.add(ConnectionError(error));
return;
}
_messagesController.addError(error);
Comment on lines 276 to 281
Copy link
Contributor Author

@dehypnosis dehypnosis Aug 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI; With this logic, a message stream won't be closed with temporary connection issue.
So the first issue can be solved.

});

Expand Down Expand Up @@ -405,6 +417,7 @@ class WebSocketLink extends Link {
await _channel?.sink.close(websocket_status.normalClosure);
_connectionStateController.add(ConnectionState.closed);
await _connectionStateController.close();
await _connectionErrorController.close();
await _messagesController.close();
_disposedCompleter!.complete();
}
Expand Down
Loading