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.
The
HttpRequest
indart:io
implementsStream<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. Thehttp_server
package at https://pub.dartlang.org/packages/http_server contains a number of utility classes to handle HTTP.WebSocket
implements bothStream
andStreamSink
. When listeming on the stream the objects received will be of typeString
orList<int>
depending on whether a string message or a binary data message was sent. Likewise you can add objects of typeString
andList<int>
to theWebSocket
to send messages.The binary data received will be of type
Uint8List
from thedart:typed_data
library. I will recommend to useUint8List
when sending binary data.As
WebSocket
uses streams all the features for streams including transformation are available.