BSON decoding from Blob
I am using Tornado to work with a client application using Javascript. BSON is used for data exchange. Since Tornado uses JSON to send data, I wrote my function to send over Websocket:
def write_bson(self, message):
message = BSON.encode(message)
self.ws_connection.write_message(message, binary=True)
Since "binary = True" in the browser I am getting Blob and don't understand how to do BSON, decode the received message.
I tried the following way to do the decoding, in the comments I indicated the console.log output:
t = new WebSocket(url);
t.onmessage = function(event) {
console.log(event.data); // Blob { size: 390, type: "" }
console.log(BSON.deserialize(event.data)); // Error: corrupt bson message
var reader = new FileReader();
reader.onload = function(e) {
console.log(e.target.result); // ArrayBuffer { byteLength: 390 }
};
var data = reader.readAsArrayBuffer(event.data);
console.log(BSON.deserialize(data)); // Error: corrupt bson message
How to decode BSON?
+3
source to share