Skip to content

Commit

Permalink
Merge pull request #185 from DLu/bson
Browse files Browse the repository at this point in the history
BSON Decoding of Binary Objects
  • Loading branch information
rctoris committed Jul 16, 2015
2 parents 9481395 + 3a5a568 commit 3e25e4e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
5 changes: 4 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"globals": {
"global": true
"global": true,
"Blob": true,
"FileReader": true,
"bson": true
},
"curly": true,
"eqeqeq": true,
Expand Down
30 changes: 26 additions & 4 deletions src/core/SocketAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
var Canvas = require('canvas');
var Image = Canvas.Image || global.Image;
var WebSocket = require('ws');
var BSON = null;
if(typeof bson !== 'undefined'){
BSON = bson().BSON;
}

/**
* If a message was compressed as a PNG image (a compression hack since
Expand Down Expand Up @@ -106,11 +110,29 @@ function SocketAdapter(client) {
* @param message - the raw JSON message from rosbridge.
*/
onmessage: function onMessage(message) {
var data = JSON.parse(typeof message === 'string' ? message : message.data);
if (data.op === 'png') {
decompressPng(data, handleMessage);
if(typeof Blob !== 'undefined' && message.data instanceof Blob) {
if(!BSON){
throw 'Cannot process BSON encoded message without BSON header.';
}
var reader = new FileReader();
reader.onload = function() {
var uint8Array = new Uint8Array(this.result);
var msg = BSON.deserialize(uint8Array);

if (msg.op === 'png') {
decompressPng(msg, handleMessage);
} else {
handleMessage(msg);
}
};
reader.readAsArrayBuffer(message.data);
} else {
handleMessage(data);
var data = JSON.parse(typeof message === 'string' ? message : message.data);
if (data.op === 'png') {
decompressPng(data, handleMessage);
} else {
handleMessage(data);
}
}
}
};
Expand Down

0 comments on commit 3e25e4e

Please sign in to comment.