getStream method

Future<Stream<List<int>>> getStream(
  1. String clientId,
  2. RequestOptions requestOptions
)

Implementation

Future<Stream<List<int>>> getStream(
  String clientId,
  RequestOptions requestOptions,
) async {
  final controller = StreamController<List<int>>();
  await _hostApi.registerStream(requestOptions.id!);
  var _requestOptions = jsonEncode(
    RequestOptionsDTO.fromDataModel(requestOptions).toJson(),
  );
  final EventChannel _eventChannel = EventChannel(
    "StreamResponseListener${requestOptions.id}",
  );
  _eventChannel.receiveBroadcastStream().listen(
    (data) {
      if (data is List<int>) {
        controller.add(data);
      } else {
        controller.close();
      }
    },
    onDone: () {
      controller.close();
    },
    onError: (error) {
      print("onError: ${error}");
    },
  );

  _hostApi.stream(clientId, _requestOptions);
  return controller.stream;
}