HtmlSseChannel constructor

HtmlSseChannel(
  1. String url,
  2. List<String>? events
)

Implementation

HtmlSseChannel(String url, List<String>? events) {
  source = EventSource(url, withCredentials: false);
  source.onOpen.first.whenComplete(() {
    _onConnected.complete();
  });

  // listen for specific message types
  source.addEventListener("message", _onMessage);
  events?.forEach((type) => source.addEventListener(type, _onMessage));

  source.onOpen.listen((_) => _timer?.cancel());
  source.onError.listen((error) {
    // By default the SSE client uses keep-alive.
    // Allow for a retry to connect before giving up.
    if (!(_timer?.isActive ?? false)) {
      _timer =
          Timer(const Duration(seconds: 5), () => _closeWithError(error));
    }
  });
}