YUKI Hiroshi
null+****@clear*****
Tue Mar 18 14:49:40 JST 2014
YUKI Hiroshi 2014-03-18 14:49:40 +0900 (Tue, 18 Mar 2014) New Revision: 7e38913102c9c49f60290a062ffb82c245de2fd9 https://github.com/droonga/express-droonga/commit/7e38913102c9c49f60290a062ffb82c245de2fd9 Message: Accept connections from multiple Droonga Engines Modified files: lib/droonga-protocol/receiver.js Modified: lib/droonga-protocol/receiver.js (+20 -6) =================================================================== --- lib/droonga-protocol/receiver.js 2014-03-17 12:39:50 +0900 (d29d36f) +++ lib/droonga-protocol/receiver.js 2014-03-18 14:49:40 +0900 (ea6c493) @@ -7,6 +7,7 @@ var msgpack = require('msgpack'); function MsgPackReceiver(port) { EventEmitter.call(this); this.port = port || undefined; + this._connections = []; this._init(); } @@ -18,9 +19,19 @@ MsgPackReceiver.prototype._init = function() { }; MsgPackReceiver.prototype._onConnect = function(socket) { - this._socket = socket; - this._messageStream = new msgpack.Stream(socket); - this._messageStream.on('msg', this._onMessageReceive.bind(this)); + var connection = { + socket: socket, + messageStream: new msgpack.Stream(socket) + }; + connection.messageStream.on('msg', this._onMessageReceive.bind(this)); + this._connections.push(connection); + + socket.on('close', (function() { + connection.messageStream.removeAllListeners('msg'); + var index = this._connections.indexOf(connection); + if (index > -1) + this._connections.splice(index, 1); + }).bind(this)); }; MsgPackReceiver.prototype._onMessageReceive = function(data) { @@ -40,17 +51,20 @@ MsgPackReceiver.prototype.listen = function(callback) { }; MsgPackReceiver.prototype.close = function(callback) { - if (this._messageStream) { - this._messageStream = undefined; - } if (this._socket) { this._socket.end(); this._socket = undefined; } if (this._server) { this._server.close(callback); + this._connections.forEach(function(connection) { + connection.socket.destroy(); + }); this._server = undefined; + } else if (callback) { + callback(); } + this._connections = []; this.port = undefined; }; -------------- next part -------------- HTML����������������������������...Download