From 6904873b871c71cb5c3fb59b1266622cebd4f75e Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Sat, 14 Dec 2013 12:44:47 +0100 Subject: [PATCH] Emitting 'clientDisconnected' in case of error. Fixes #79. --- lib/client.js | 6 ++---- test/abstract_server.js | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) 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([