diff --git a/lib/handlers/index.js b/lib/handlers/index.js index c188a244..d4db8345 100644 --- a/lib/handlers/index.js +++ b/lib/handlers/index.js @@ -10,24 +10,16 @@ var handlePubrec = require('./pubrec') var handlePing = require('./ping') function handle (client, packet, done) { - if ( - // [MQTT-3.1.0-1] - (packet.cmd !== 'connect' && !client.connected) || + if (packet.cmd === 'connect') { // [MQTT-3.1.0-2] - (packet.cmd === 'connect' && client.connected) - ) { - client.conn.destroy() - return + return client.connected ? client.conn.destroy() : handleConnect(client, packet, done) } - - if (packet.cmd !== 'connect' && client._keepaliveInterval > 0) { - client._keepaliveTimer.reschedule(client._keepaliveInterval) + if (!client.connected) { + // [MQTT-3.1.0-1] + return client.conn.destroy() } switch (packet.cmd) { - case 'connect': - handleConnect(client, packet, done) - break case 'publish': handlePublish(client, packet, done) break @@ -53,9 +45,14 @@ function handle (client, packet, done) { case 'disconnect': client.disconnected = true client.conn.end() - break + return default: client.conn.destroy() + return + } + + if (client._keepaliveInterval > 0) { + client._keepaliveTimer.reschedule(client._keepaliveInterval) } } diff --git a/test/handlers/connect.js b/test/handlers/connect.js index fb4173ad..502ee9c7 100644 --- a/test/handlers/connect.js +++ b/test/handlers/connect.js @@ -2,7 +2,7 @@ var test = require('tape').test var EE = require('events').EventEmitter -var handleConnect = require('../../lib/handlers/connect') +var handle = require('../../lib/handlers/index') test('reject clients with no clientId running on MQTT 3.1', function (t) { t.plan(2) @@ -13,12 +13,18 @@ test('reject clients with no clientId running on MQTT 3.1', function (t) { } client.broker = broker + client.conn = { + destroy: function () { + t.fail('should not destroy') + } + } client.on('error', function (err) { t.equal(err.message, 'Empty clientIds are supported only on MQTT 3.1.1', 'error message') }) - handleConnect(client, { + handle(client, { + cmd: 'connect', protocolVersion: 3, protocolId: 'MQIsdp' }, function (err) {