From Blob to ByteBuffer

1,221 views
Skip to first unread message

Ernest Micklei

unread,
Jun 7, 2014, 5:35:58 PM6/7/14
to mi...@dartlang.org
Hi,

In Dart, I am using a websocket to receive binary data (protobuf encoded).
From the MessageEvent, I receive an instance of Blob.

How can I access its bytes in order to unmarshall my message? (for that I need a ByteBuffer or Uint8List)

Thanks
^ Ernest

Günter Zöchbauer

unread,
Jun 8, 2014, 6:19:42 AM6/8/14
to mi...@dartlang.org
I think the code example in this question should work for your use case too

Ernest Micklei

unread,
Jun 8, 2014, 7:42:42 AM6/8/14
to mi...@dartlang.org
Thx, based on that I figured to set the binaryType of the socket:

ws.binaryType = "arraybuffer";

Now, I get an List<int> (Uint8List) from the MessageEvent data.



--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new



--
^ Ernest Micklei

Ernest Micklei

unread,
Jun 9, 2014, 10:49:04 AM6/9/14
to mi...@dartlang.org
I noticed that running the app inside a browser (after dart2js 1.2.0) 
the MessageEvent data is a ByteBuffer and causes runtime errors somewhere. 

I am not sure whether it is a Dart protobuf issue or dart2js. Suggestions anybody?

Ernest Micklei

unread,
Jun 9, 2014, 11:35:16 AM6/9/14
to mi...@dartlang.org
My workaround:

    if (e.data is Uint8List) {

      // Dartium

      mm = new MyMessage.fromBuffer(e.data);

    } else {

      // Javascript

      ByteBuffer buf = e.data as ByteBuffer;

      mm = new MyMessage.fromBuffer(new Uint8List.view(buf));

Emily Fortuna

unread,
Jun 11, 2014, 12:06:13 PM6/11/14
to mi...@dartlang.org
Hi Ernest,
You're right, that looks like a bug in the dart:html library. I've filed a bug about it here: https://code.google.com/p/dart/issues/detail?id=19364
Emily
Reply all
Reply to author
Forward
0 new messages