What is dart concept about data streams?

596 Views Asked by At

Dart changing rapidly so I did not find a way to get data stream from HttpResponse to send arbitrary (even binary) data via websocket. Even file IO has no openInput/OutputStream for now. What is concept for working with data streams in dart at this time?

I'd actually like to get kind of abstract data stream object (like System.IO.Stream in C#) to implement serialization interface with it and then apply it to websocket data stream, so I can change serialization format for my network packets with new serializer implementations in future, or create memory stream object (like System.IO.MemoryStream in C#) to get serialized data in memory.

And finally - is it possible to send binary data with websockets at all?

Thanx.

1

There are 1 best solutions below

2
On

The HttpRequest in dart:io implements Stream<List<int>>. To get the data listen on the stream. An example has recently been added to the API doc at http://api.dartlang.org/docs/releases/latest/dart_io/HttpRequest.html. The http_server package at https://pub.dartlang.org/packages/http_server contains a number of utility classes to handle HTTP.

WebSocket implements both Stream and StreamSink. When listeming on the stream the objects received will be of type String or List<int> depending on whether a string message or a binary data message was sent. Likewise you can add objects of type String and List<int> to the WebSocket to send messages.

The binary data received will be of type Uint8List from the dart:typed_data library. I will recommend to use Uint8List when sending binary data.

As WebSocket uses streams all the features for streams including transformation are available.