diff --git a/lib/client.js b/lib/client.js index 548f392..d874635 100644 --- a/lib/client.js +++ b/lib/client.js @@ -510,13 +510,16 @@ 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() { that.server.persistClient(that); - if(that.connection.stream.destroyed){ - if(callback) { - callback(); - } + if(that._streamClosedRequiresCleanup){ + cleanup(); } else { that.connection.stream.on('end', cleanup); that.connection.stream.end();