From: Darren Date: Thu, 27 Jun 2013 12:59:35 +0000 (+0100) Subject: Correctly triggering IRC socket close events X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=5e211c3dc00cbf9e7d43ed64261b5daf28c6bc8a;p=KiwiIRC.git Correctly triggering IRC socket close events --- diff --git a/server/irc/connection.js b/server/irc/connection.js index 407e6e9..78e1430 100644 --- a/server/irc/connection.js +++ b/server/irc/connection.js @@ -219,6 +219,8 @@ IrcConnection.prototype.end = function (data, callback) { * Clean up this IrcConnection instance and any sockets */ IrcConnection.prototype.dispose = function () { + var that = this; + _.each(this.irc_users, function (user) { user.dispose(); }); @@ -233,8 +235,20 @@ IrcConnection.prototype.dispose = function () { EventBinder.unbindIrcEvents('', this.irc_events, this); - this.disposeSocket(); - this.removeAllListeners(); + // If we're still connected, wait until the socket is closed before disposing + // so that all the events are still correctly triggered + if (this.socket && this.connected) { + this.socket.once('close', function() { + that.disposeSocket(); + that.removeAllListeners(); + }); + + this.socket.end(); + + } else { + this.disposeSocket(); + this.removeAllListeners(); + } };