Fix connection double closing.

This commit is contained in:
Julien Fontanet 2015-02-03 14:33:39 +01:00
parent d2d401883e
commit bc9975baa1
2 changed files with 12 additions and 4 deletions

View File

@ -12,26 +12,34 @@ var assign = require('lodash.assign');
var has = Object.prototype.hasOwnProperty; var has = Object.prototype.hasOwnProperty;
has = has.call.bind(has); has = has.call.bind(has);
function noop() {}
//==================================================================== //====================================================================
var Connection = function Connection(opts) { function Connection(opts) {
EventEmitter.call(this);
this.data = Object.create(null); this.data = Object.create(null);
this._close = opts.close; this._close = opts.close;
this.notify = opts.notify; this.notify = opts.notify;
}; }
inherits(Connection, EventEmitter); inherits(Connection, EventEmitter);
assign(Connection.prototype, { assign(Connection.prototype, {
// Close the connection. // Close the connection.
close: function () { close: function () {
// Prevent errors when the connection is closed more than once.
this.close = noop;
this._close(); this._close();
this.emit('close'); this.emit('close');
// Releases values AMAP to ease the garbage collecting. // Releases values AMAP to ease the garbage collecting.
for (var key in this) for (var key in this)
{ {
if (has(this, key)) if (key !== 'close' && has(this, key))
{ {
delete this[key]; delete this[key];
} }

View File

@ -158,7 +158,7 @@ exports = module.exports = $coroutine (args) ->
# Close the connection with the socket. # Close the connection with the socket.
socket.on 'close', -> socket.on 'close', ->
$debug '- WebSocket connection' $debug '- WebSocket connection'
$bind connection.close, connection connection.close()
return return
# Connect the WebSocket to the JSON-RPC server # Connect the WebSocket to the JSON-RPC server