From c98125b06547e3c1c16ca887e8ac3461513f0c46 Mon Sep 17 00:00:00 2001 From: Brian Carlson Date: Sat, 17 Aug 2013 17:25:24 -0500 Subject: [PATCH] Use on('data') for v0.8.x --- lib/connection.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) 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) {