Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed message id generation according to spec #138

Merged
merged 1 commit into from
Nov 21, 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
9 changes: 8 additions & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ function MqttClient(streamBuilder, options) {
this.disconnecting = false;
// Reconnect timer
this.reconnectTimer = null;
// MessageIDs starting with 1
this.nextId = 1;

// Inflight messages
this.inflight = {
Expand Down Expand Up @@ -578,5 +580,10 @@ MqttClient.prototype._handlePubrel = function(packet) {
* _nextId
*/
MqttClient.prototype._nextId = function() {
return Math.floor(Math.random() * 65535);
var id = this.nextId++;
// Ensure 16 bit unsigned int:
if (id === 65535) {
this.nextId = 1;
}
return id;
};
24 changes: 24 additions & 0 deletions test/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,28 @@ var server = mqtt.createServer(function (client) {

describe('MqttClient', function() {
abstractClientTests(server, createClient, port);

describe('_nextId', function() {

it('should return 1 on first call', function() {
var client = createClient();

client._nextId().should.equal(1);
}),

it('should return 2 on second call', function() {
var client = createClient();
client._nextId();

client._nextId().should.equal(2);
}),

it('should return 1 once the interal counter reached limit', function() {
var client = createClient();
client.nextId = 65535;

client._nextId().should.equal(65535);
client._nextId().should.equal(1);
})
})
});