diff --git a/lib/client.js b/lib/client.js index f94f55c..8d674ea 100644 --- a/lib/client.js +++ b/lib/client.js @@ -455,6 +455,8 @@ Client.prototype.handleAuthorizePublish = function(err, success, packet) { Client.prototype.onNonDisconnectClose = function() { var that = this, logger = that.logger; + that._streamClosedRequiresCleanup = true; + if (that.will) { logger.info({ willTopic: that.will.topic }, "delivering last will"); that.server.ascoltatore.publish( @@ -512,10 +514,6 @@ Client.prototype.close = function(callback) { } }; - that.connection.stream.on('end' , function(){ - that._streamClosedRequiresCleanup = true; - }); - that._closing = true; async.parallel(Object.keys(that.subscriptions).map(that.unsubscribeMapTo.bind(that)), function() { diff --git a/test/abstract_server.js b/test/abstract_server.js index aaf92b4..581004f 100644 --- a/test/abstract_server.js +++ b/test/abstract_server.js @@ -649,6 +649,23 @@ module.exports = function(moscaSettings, createConnection) { client.connect(buildOpts()); }); + it("should emit an event when a client is disconnected without a disconnect", function(done) { + var client = createConnection(settings.port, settings.host); + + instance.once('clientDisconnected', function(serverClient) { + expect(serverClient).not.to.be.equal(undefined); + done(); + }); + + client.on('error', done); + + client.on('connack', function() { + client.stream.end(); + }); + + client.connect(buildOpts()); + }); + it("should emit a ready and closed events", function(done) { var server = new mosca.Server(moscaSettings()); async.series([