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

Add retry logic #17

Merged
merged 3 commits into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 3.0.0

- Add retry logic.

** Possible Breaking Change ** Error messages may now be delayed up to 5 seconds
Copy link
Member

Choose a reason for hiding this comment

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

Should we say "Error messages for disconnects"?

Copy link
Member Author

Choose a reason for hiding this comment

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

General error messages may be delayed as we have no way to make a distinction.

in the client.

## 2.1.2

- Remove `package:http` dependency.
Expand Down
17 changes: 16 additions & 1 deletion lib/client/sse_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class SseClient extends StreamChannelMixin<String> {

String _serverUrl;

Timer _errorTimer;

/// [serverUrl] is the URL under which the server is listening for
/// incoming bi-directional SSE connections.
SseClient(String serverUrl) {
Expand All @@ -36,7 +38,20 @@ class SseClient extends StreamChannelMixin<String> {
.listen(_onOutgoingMessage, onDone: _onOutgoingDone);
_eventSource.addEventListener('message', _onIncomingMessage);
_eventSource.addEventListener('control', _onIncomingControlMessage);
_eventSource.onError.listen(_incomingController.addError);
_eventSource.onOpen.listen((_) {
_errorTimer?.cancel();
});
_eventSource.onError.listen((error) {
if (!(_errorTimer?.isActive ?? false)) {
// By default the SSE client uses keep-alive.
// Allow for a retry to connect before giving up.
_errorTimer = Timer(const Duration(seconds: 5), () {
_incomingController.addError(error);
_eventSource.close();
});
}
});

_startPostingMessages();
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: sse
version: 2.1.2
version: 3.0.0
author: Dart Team <[email protected]>
homepage: https://github.com/dart-lang/sse
description: >-
Expand Down
Loading