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

Pass packet and client to ascoltatore in mosca property #30

Merged
merged 2 commits into from
Jun 10, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ Server.prototype.serve = function(client) {
topic: topic,
payload: payload,
qos: qos,
messageId: (options && options.messageId) || client.nextId++
messageId: client.nextId++
};

actualSend(packet, 0);
Expand Down Expand Up @@ -384,7 +384,13 @@ Server.prototype.serve = function(client) {
that.ascoltatore.publish(
packet.topic,
packet.payload,
{ qos: packet.qos, clientId: client.id, messageId: packet.messageId },
{
qos: packet.qos,
mosca: {
client: client, // the client object
packet: packet // the packet being sent
}
},
function() {
debug("client " + client.id + " published packet to topic " + packet.topic);

Expand Down
66 changes: 37 additions & 29 deletions test/server_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -709,35 +709,6 @@ describe("mosca.Server", function() {
});
});

it("should receive published messageId", function(done) {
buildAndConnect(done, function(client) {

client.once("publish", function(packet) {
expect(packet.messageId).to.be.equal(24);
client.disconnect();
});

client.on("suback", function(packet) {
client.publish({
topic: "hello",
qos: 1,
messageId: 24
});
});

var subscriptions = [{
topic: "hello",
qos: 1
}
];

client.subscribe({
subscriptions: subscriptions,
messageId: 42
});
});
});

it("should receive all messages at QoS 0 if a subscription is done with QoS 0", function(done) {
buildAndConnect(done, function(client) {

Expand Down Expand Up @@ -891,6 +862,43 @@ describe("mosca.Server", function() {
]);
});

it("should pass mosca options to backend when publishing", function(done) {
instance.authenticate = function(client, username, password, callback) {
expect(username).to.be.eql("matteo");
expect(password).to.be.eql("collina");
client.user = username;
callback(null, true);
};

buildClient(done, function(client) {

var messageId = Math.floor(65535 * Math.random());

instance.ascoltatore.subscribe("hello", function (topic, message, options) {
expect(options.mosca.packet).to.have.property("messageId", messageId);
expect(options.mosca.client).to.have.property("user", "matteo");
client.disconnect();
});

var options = buildOpts();
options.username = "matteo";
options.password = "collina";

client.connect(options);

client.on('connack', function(packet) {
expect(packet.returnCode).to.eql(0);

client.publish({
topic: "hello",
qos: 1,
payload: "world",
messageId: messageId
});
});
});
});

it("should support authentication (success)", function(done) {
instance.authenticate = function(client, username, password, callback) {
expect(username).to.be.eql("matteo");
Expand Down