IOSseClient.connect constructor

IOSseClient.connect(
  1. Uri uri
)

Implementation

factory IOSseClient.connect(Uri uri) {
  late StreamController<String?> incomingController;
  final client = Client();

  incomingController = StreamController<String?>.broadcast(onListen: () {
    var request = Request('GET', uri)
      ..headers['Accept'] = 'text/event-stream';

    client.send(request).then((response) {
      if (response.statusCode == 200) {
        response.stream.transform(EventSourceTransformer()).listen((event) {
          incomingController.sink.add(event.data);
        });
      } else {
        incomingController
            .addError(Exception('Failed to connect to ${uri.toString()}'));
      }
    });
  }, onCancel: () {
    incomingController.close();
  });

  return IOSseClient(incomingController.stream);
}