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

Fix exponential retries on parsing error #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
149 changes: 88 additions & 61 deletions lib/flutter_client_sse.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,70 +77,97 @@ class SSEClient {
Future<http.StreamedResponse> response = _client.send(request);

/// Listening to the response as a stream
response.asStream().listen((data) {
late StreamSubscription<http.StreamedResponse>
responseStreamSubscription;
responseStreamSubscription = response.asStream().listen((data) async {
if (data.headers["content-type"] != "text/event-stream") {
print("---ERROR---");
if (data.headers["connection"] != "keep-alive") {
final responseText =
await data.stream.transform(Utf8Decoder()).reduce(
(previous, element) => previous + element,
);
print(responseText);
}
_retryConnection(
method: method,
url: url,
header: header,
streamController: streamController,
);
responseStreamSubscription.cancel();
return;
}

/// Applying transforms and listening to it
data.stream
..transform(Utf8Decoder()).transform(LineSplitter()).listen(
(dataLine) {
if (dataLine.isEmpty) {
/// This means that the complete event set has been read.
/// We then add the event to the stream
streamController.add(currentSSEModel);
currentSSEModel = SSEModel(data: '', id: '', event: '');
return;
}
late StreamSubscription<String> transformedResponseStreamSubscription;
transformedResponseStreamSubscription = data.stream
.transform(Utf8Decoder())
.transform(LineSplitter())
.listen(
(dataLine) {
if (dataLine.isEmpty) {
/// This means that the complete event set has been read.
/// We then add the event to the stream
streamController.add(currentSSEModel);
currentSSEModel = SSEModel(data: '', id: '', event: '');
return;
}

/// Get the match of each line through the regex
Match match = lineRegex.firstMatch(dataLine)!;
var field = match.group(1);
if (field!.isEmpty) {
return;
}
var value = '';
if (field == 'data') {
// If the field is data, we get the data through the substring
value = dataLine.substring(
5,
);
} else {
value = match.group(2) ?? '';
}
switch (field) {
case 'event':
currentSSEModel.event = value;
break;
case 'data':
currentSSEModel.data =
(currentSSEModel.data ?? '') + value + '\n';
break;
case 'id':
currentSSEModel.id = value;
break;
case 'retry':
break;
default:
print('---ERROR---');
print(dataLine);
_retryConnection(
method: method,
url: url,
header: header,
streamController: streamController,
);
}
},
onError: (e, s) {
print('---ERROR---');
print(e);
_retryConnection(
method: method,
url: url,
header: header,
body: body,
streamController: streamController,
/// Get the match of each line through the regex
Match match = lineRegex.firstMatch(dataLine)!;
var field = match.group(1);
if (field!.isEmpty) {
return;
}
var value = '';
if (field == 'data') {
// If the field is data, we get the data through the substring
value = dataLine.substring(
5,
);
},
);
} else {
value = match.group(2) ?? '';
}
switch (field) {
case 'event':
currentSSEModel.event = value;
break;
case 'data':
currentSSEModel.data =
(currentSSEModel.data ?? '') + value + '\n';
break;
case 'id':
currentSSEModel.id = value;
break;
case 'retry':
break;
default:
print("---ERROR---");
print(dataLine);
transformedResponseStreamSubscription.cancel();
responseStreamSubscription.cancel();

_retryConnection(
method: method,
url: url,
header: header,
streamController: streamController,
);
}
},
onError: (e, s) {
print('---ERROR---');
print(e);
_retryConnection(
method: method,
url: url,
header: header,
body: body,
streamController: streamController,
);
},
);
}, onError: (e, s) {
print('---ERROR---');
print(e);
Expand Down