Skip to content

Commit

Permalink
Add support for custom encoding/decoding. (#397)
Browse files Browse the repository at this point in the history
* Add support for custom encoding/decoding.

* Remove whitespace

Co-authored-by: Matthijs van der Burgh <[email protected]>
Co-authored-by: Jihoon Lee <[email protected]>
  • Loading branch information
3 people authored Apr 22, 2021
1 parent 72aca8d commit 21a32ec
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
27 changes: 18 additions & 9 deletions src/core/Ros.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ var EventEmitter2 = require('eventemitter2').EventEmitter2;
*/
function Ros(options) {
options = options || {};
var that = this;
this.socket = null;
this.idCounter = 0;
this.isConnected = false;
this.transportLibrary = options.transportLibrary || 'websocket';
this.transportOptions = options.transportOptions || {};
this._sendFunc = function(msg) { that.sendEncodedMessage(msg) }

if (typeof options.groovyCompatibility === 'undefined') {
this.groovyCompatibility = true;
Expand Down Expand Up @@ -120,14 +122,9 @@ Ros.prototype.authenticate = function(mac, client, dest, rand, t, level, end) {
this.callOnConnection(auth);
};

/**
* Sends the message over the WebSocket, but queues the message up if not yet
* connected.
*/
Ros.prototype.callOnConnection = function(message) {
var that = this;
var messageJson = JSON.stringify(message);
Ros.prototype.sendEncodedMessage= function(messageEncoded) {
var emitter = null;
var that = this;
if (this.transportLibrary === 'socket.io') {
emitter = function(msg){that.socket.emit('operation', msg);};
} else {
Expand All @@ -136,10 +133,22 @@ Ros.prototype.callOnConnection = function(message) {

if (!this.isConnected) {
that.once('connection', function() {
emitter(messageJson);
emitter(messageEncoded);
});
} else {
emitter(messageJson);
emitter(messageEncoded);
}
}

/**
* Sends the message over the WebSocket, but queues the message up if not yet
* connected.
*/
Ros.prototype.callOnConnection = function(message) {
if (this.transportOptions.encoder) {
this.transportOptions.encoder(message, this._sendFunc);
} else {
this._sendFunc(JSON.stringify(message));
}
};

Expand Down
12 changes: 11 additions & 1 deletion src/core/SocketAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ if(typeof bson !== 'undefined'){
* @private
*/
function SocketAdapter(client) {

var decoder = null
if (client.transportOptions.decoder) {
decoder = client.transportOptions.decoder
}

function handleMessage(message) {
if (message.op === 'publish') {
client.emit(message.topic, message.msg);
Expand Down Expand Up @@ -103,7 +109,11 @@ function SocketAdapter(client) {
* @memberof SocketAdapter
*/
onmessage: function onMessage(data) {
if (typeof Blob !== 'undefined' && data.data instanceof Blob) {
if (decoder) {
decoder(data.data, function (message) {
handleMessage(message);
});
} else if (typeof Blob !== 'undefined' && data.data instanceof Blob) {
decodeBSON(data.data, function (message) {
handlePng(message, handleMessage);
});
Expand Down

0 comments on commit 21a32ec

Please sign in to comment.