Skip to content

Commit

Permalink
Allow multiple subscriptions to a topic
Browse files Browse the repository at this point in the history
  • Loading branch information
gyeates committed Oct 1, 2014
1 parent 4894b08 commit 19558dd
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/core/Topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ Topic.prototype.__proto__ = EventEmitter2.prototype;
Topic.prototype.subscribe = function(callback) {
var that = this;
this.on('message', callback);

if (this.subscribeId) { return; }
this.ros.on(this.name, function(data) {
var message = new Message(data);
that.emit('message', message);
});

this.subscribeId = 'subscribe:' + this.name + ':' + (++this.ros.idCounter);
this.ros.callOnConnection({
op: 'subscribe',
Expand All @@ -78,12 +79,14 @@ Topic.prototype.subscribe = function(callback) {
* all subscribe callbacks.
*/
Topic.prototype.unsubscribe = function() {
if (!this.subscribeId) { return; }
this.ros.removeAllListeners([this.name]);
this.ros.callOnConnection({
op: 'unsubscribe',
id: this.subscribeId,
topic: this.name
});
this.subscribeId = null;
};

/**
Expand Down

0 comments on commit 19558dd

Please sign in to comment.