diff --git a/lib/client.js b/lib/client.js index 1a01cab..2aa3b05 100644 --- a/lib/client.js +++ b/lib/client.js @@ -83,11 +83,11 @@ Client.prototype.setSocket = function(socket) { var self = this; function afterParse(err, parsed) { if (err || (parsed && parsed.error)) { - this.emit('error', err || parser.error); + this.emit('error', err || parsed.error); this.end("ProtocolError"); return; } - if (! parsed) { cont = false; return; } + if (! parsed) { return; } packet = parsed.results; incomingBuffer = incomingBuffer.slice(parsed.size); @@ -96,8 +96,16 @@ Client.prototype.setSocket = function(socket) { self.emit('packet', packet); self.emit('raw.' + packetName, parsed.buffer); self.emit('raw', parsed.buffer); + prepareParse(); } - var cont = true; + + function prepareParse() { + if(self.compressionThreshold == -2) + afterParse(null, parsePacket(incomingBuffer, self.state, self.isServer, self.packetsToParse)); + else + parseNewStylePacket(incomingBuffer, self.state, self.isServer, self.packetsToParse, afterParse); + } + self.socket = socket; if (self.socket.setNoDelay) self.socket.setNoDelay(true); @@ -105,12 +113,7 @@ Client.prototype.setSocket = function(socket) { self.socket.on('data', function(data) { if (self.encryptionEnabled) data = new Buffer(self.decipher.update(data), 'binary'); incomingBuffer = Buffer.concat([incomingBuffer, data]); - while (cont) { - if(self.compressionThreshold == -2) - afterParse(null, parsePacket(incomingBuffer, self.state, self.isServer, self.packetsToParse)); - else - parseNewStylePacket(incomingBuffer, self.state, self.isServer, self.packetsToParse, afterParse); - } + prepareParse() }); self.socket.on('connect', function() {