Closed skaba closed 2 months ago
Hi @skaba. Thank you for the suggestion. Will work on it and update the package.
Hello, do you have any update on this ?
Hi, how can I catch error when the server disconnects or not avalible on connect
Hi, Any updates on this?
First of all, Thanks to @pratikbaid3 for this package. This issue led me to create my own package with better error handling and reconnection logic.
Check it out, EventFlux.
await Future.delayed(Duration(seconds: 5)); // wait be
Stream<Map<String, dynamic>> _connectToSse() async* { while (true) { try { final request = await http.Client().send(http.Request('GET', Uri.parse('http://your-sse-server.com'))); final response = await http.Response.fromStream(request);
if (response.statusCode != 200) throw Exception('Failed to connect to SSE server');
yield* utf8.decoder.bind(response.stream).transform(const LineSplitter()).map((line) {
try {
return jsonDecode(line);
} catch (e) {
print('Failed to decode JSON: $e');
return null;
}
}).where((json) => json != null);
} catch (e) {
print('Connection to SSE server lost. Attempting to reconnect...');
await Future.delayed(Duration(seconds: 5)); // wait before attempting to reconnect
}
}
}
Currently when server closes connection stream is ended, can we make it reconnect?