Skip to content
This repository has been archived by the owner on Feb 11, 2020. It is now read-only.

Commit

Permalink
Fixed wildcards subscribing
Browse files Browse the repository at this point in the history
Closes #7.
  • Loading branch information
mcollina committed Feb 20, 2013
1 parent fc24642 commit 37b29b7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ Server.prototype.serve = function (client) {

async.parallel(packet.subscriptions.map(function(s) {
return function(cb) {
that.ascoltatore.subscribe(s.topic, forward, function() {
that.ascoltatore.subscribe(s.topic.replace("#", "*"), forward, function() {
debug("subscribed " + client.id + " to " + s.topic);
cb();
});
Expand All @@ -178,7 +178,7 @@ Server.prototype.serve = function (client) {
async.parallel(packet.unsubscriptions.map(function(topic) {
return function(cb) {
debug("unsubscribed " + client.id + " from " + topic);
that.ascoltatore.unsubscribe(topic, forward, cb);
that.ascoltatore.unsubscribe(topic.replace("#", "*"), forward, cb);
};
}), function() {
client.unsuback({ messageId: packet.messageId });
Expand Down
27 changes: 24 additions & 3 deletions test/server_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ describe(mosca.Server, function() {
var messageId = Math.floor(65535 * Math.random());

client.on("unsuback", function(packet) {
client.disconnect();
expect(packet).to.have.property("messageId", messageId);
client.disconnect();
});

client.on("suback", function(packet) {
Expand Down Expand Up @@ -260,7 +260,7 @@ describe(mosca.Server, function() {
it("should emit an event when a client is disconnected", function(done) {
mqtt.createClient(settings.port, settings.host, function(err, client) {
if(err) {
done(err)
done(err);
return;
}

Expand All @@ -274,7 +274,7 @@ describe(mosca.Server, function() {
client.on('connack', function() {
client.disconnect();
});

client.connect();
});
});
Expand Down Expand Up @@ -321,4 +321,25 @@ describe(mosca.Server, function() {
}
], done);
});

it("should support subscribing to wildcards", function(done) {
var d = donner(2, done);
buildAndConnect(d, function(client1) {

client1.on("publish", function(packet) {
expect(packet.topic).to.be.equal("hello/world");
expect(packet.payload).to.be.equal("some data");
client1.disconnect();
});

client1.on("suback", function() {
buildAndConnect(d, function(client2) {
client2.publish({ topic: "hello/world", payload: "some data" });
client2.disconnect();
});
});

client1.subscribe({ topic: "hello/#" });
});
});
});

0 comments on commit 37b29b7

Please sign in to comment.