Skip to content

Commit

Permalink
Add retry logic (flutter#17)
Browse files Browse the repository at this point in the history
* Add retry logic
  • Loading branch information
grouma authored Nov 15, 2019
1 parent 05f2180 commit a5e8d44
Show file tree
Hide file tree
Showing 4 changed files with 7,438 additions and 6,581 deletions.
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
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 <misc@dartlang.org>
homepage: https://github.com/dart-lang/sse
description: >-
Expand Down
Loading

0 comments on commit a5e8d44

Please sign in to comment.