Skip to content

Commit a5e8d44

Browse files
authored
Add retry logic (flutter#17)
* Add retry logic
1 parent 05f2180 commit a5e8d44

File tree

4 files changed

+7438
-6581
lines changed

4 files changed

+7438
-6581
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 3.0.0
2+
3+
- Add retry logic.
4+
5+
** Possible Breaking Change ** Error messages may now be delayed up to 5 seconds
6+
in the client.
7+
18
## 2.1.2
29

310
- Remove `package:http` dependency.

lib/client/sse_client.dart

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class SseClient extends StreamChannelMixin<String> {
2525

2626
String _serverUrl;
2727

28+
Timer _errorTimer;
29+
2830
/// [serverUrl] is the URL under which the server is listening for
2931
/// incoming bi-directional SSE connections.
3032
SseClient(String serverUrl) {
@@ -36,7 +38,20 @@ class SseClient extends StreamChannelMixin<String> {
3638
.listen(_onOutgoingMessage, onDone: _onOutgoingDone);
3739
_eventSource.addEventListener('message', _onIncomingMessage);
3840
_eventSource.addEventListener('control', _onIncomingControlMessage);
39-
_eventSource.onError.listen(_incomingController.addError);
41+
_eventSource.onOpen.listen((_) {
42+
_errorTimer?.cancel();
43+
});
44+
_eventSource.onError.listen((error) {
45+
if (!(_errorTimer?.isActive ?? false)) {
46+
// By default the SSE client uses keep-alive.
47+
// Allow for a retry to connect before giving up.
48+
_errorTimer = Timer(const Duration(seconds: 5), () {
49+
_incomingController.addError(error);
50+
_eventSource.close();
51+
});
52+
}
53+
});
54+
4055
_startPostingMessages();
4156
}
4257

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: sse
2-
version: 2.1.2
2+
version: 3.0.0
33
author: Dart Team <[email protected]>
44
homepage: https://github.com/dart-lang/sse
55
description: >-

0 commit comments

Comments
 (0)