From 37b29b7de5ecd3828e30b1862055baa172ebbdc4 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Wed, 20 Feb 2013 09:49:01 +0000 Subject: [PATCH] Fixed wildcards subscribing Closes #7. --- lib/server.js | 4 ++-- test/server_spec.js | 27 ++++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/server.js b/lib/server.js index 19957ec..0b4274a 100644 --- a/lib/server.js +++ b/lib/server.js @@ -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(); }); @@ -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 }); diff --git a/test/server_spec.js b/test/server_spec.js index dc0e9fd..0e1138e 100644 --- a/test/server_spec.js +++ b/test/server_spec.js @@ -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) { @@ -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; } @@ -274,7 +274,7 @@ describe(mosca.Server, function() { client.on('connack', function() { client.disconnect(); }); - + client.connect(); }); }); @@ -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/#" }); + }); + }); });