From 43b29cf904172cf430cf01a7d8198ed43897f92c Mon Sep 17 00:00:00 2001 From: roblabla Date: Thu, 19 Feb 2015 20:36:37 +0000 Subject: [PATCH] Loop should be recursive, otherwise messages disappear --- lib/client.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) 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() {