diff --git a/lib/connection.js b/lib/connection.js index 910e3ba..8f31dcc 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -22,6 +22,13 @@ var Connection = function(config) { this.ssl = config.ssl || false; this._ending = false; this._mode = TEXT_MODE; + this._emitMessage = false; + var self = this; + this.on('newListener', function(eventName) { + if(eventName == 'message') { + self._emitMessage = true; + } + }); }; util.inherits(Connection, EventEmitter); @@ -80,18 +87,16 @@ Connection.prototype.connect = function(port, host) { }; Connection.prototype.attachListeners = function(stream) { - var self = this; - stream.on('readable', function() { - var buff = stream.read(); - if(!buff) return; - self.setBuffer(buff); - var msg = self.parseMessage(); - while(msg) { - self.emit('message', msg); - self.emit(msg.name, msg); - msg = self.parseMessage(); + stream.on('data', function(buff) { + this.setBuffer(buff); + var msg; + while(msg = this.parseMessage()) { + if(this._emitMessage) { + this.emit('message', msg); + } + this.emit(msg.name, msg); } - }); + }.bind(this)); }; Connection.prototype.requestSsl = function(config) { @@ -401,6 +406,7 @@ Connection.prototype.parseMessage = function() { case 0x64: //d return this.parsed(buffer, length); } + return false; }; Connection.prototype.parseR = function(buffer, length) {