From e358f0d0c77a43a87ff1506ad7fed34b82454d1d Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Sat, 16 Mar 2013 16:53:26 -0400 Subject: [PATCH 01/17] add 'drain' emit to WS --- lib/transports/websocket.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/transports/websocket.js b/lib/transports/websocket.js index cb6a566ca..47b3015b9 100644 --- a/lib/transports/websocket.js +++ b/lib/transports/websocket.js @@ -82,9 +82,17 @@ WS.prototype.doOpen = function(){ */ WS.prototype.write = function(packets){ - for (var i = 0, l = packets.length; i < l; i++) { - this.socket.send(parser.encodePacket(packets[i])); - } + // encodePayload instead of encodePacket + this.writable = false; + this.socket.send(parser.encodePayload(packets)); + // check periodically if we're done sending + var bufferedAmountId = this.setInterval(function() { + if (socket.bufferedAmount == 0) { + clearInterval(bufferedAmountId); + this.writable = true; + this.emit('drain'); + } + }, 50); }; /** From bf13803a9e9242e52ae006ceaaaa23a05288c2fe Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Sat, 16 Mar 2013 17:23:52 -0400 Subject: [PATCH 02/17] make sure to stop interval if transport closes --- lib/transport.js | 5 +++-- lib/transports/websocket.js | 12 ++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/transport.js b/lib/transport.js index 7eab93b82..ed7945978 100644 --- a/lib/transport.js +++ b/lib/transport.js @@ -90,9 +90,9 @@ Transport.prototype.close = function () { * @api private */ -Transport.prototype.send = function(packets){ +Transport.prototype.send = function(packets, fn){ if ('open' == this.readyState) { - this.write(packets); + this.write(packets, fn); } else { throw new Error('Transport not open'); } @@ -137,5 +137,6 @@ Transport.prototype.onPacket = function (packet) { Transport.prototype.onClose = function () { this.readyState = 'closed'; + this.intervalCleanup && this.intervalCleanup.call(this); this.emit('close'); }; diff --git a/lib/transports/websocket.js b/lib/transports/websocket.js index 47b3015b9..338013c3c 100644 --- a/lib/transports/websocket.js +++ b/lib/transports/websocket.js @@ -29,6 +29,7 @@ var global = util.global(); function WS(opts){ Transport.call(this, opts); + this.bufferedAmountId; }; /** @@ -82,19 +83,26 @@ WS.prototype.doOpen = function(){ */ WS.prototype.write = function(packets){ + var self = this; // encodePayload instead of encodePacket this.writable = false; this.socket.send(parser.encodePayload(packets)); // check periodically if we're done sending - var bufferedAmountId = this.setInterval(function() { + this.bufferedAmountId = this.setInterval(function() { if (socket.bufferedAmount == 0) { - clearInterval(bufferedAmountId); + clearInterval(self.bufferedAmountId); this.writable = true; this.emit('drain'); } }, 50); }; +WS.prototype.intervalCleanup = function(){ + if (this.bufferedAmountId) { + clearInterval(this.bufferedAmountId); + } +} + /** * Closes socket. * From 8ea4bb9b6b4e96527c9bc2627f10a0ffd3f9cd46 Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Sat, 16 Mar 2013 18:31:58 -0400 Subject: [PATCH 03/17] feature-check for bufferedAmount --- lib/transports/websocket.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/transports/websocket.js b/lib/transports/websocket.js index 338013c3c..36a603663 100644 --- a/lib/transports/websocket.js +++ b/lib/transports/websocket.js @@ -89,7 +89,7 @@ WS.prototype.write = function(packets){ this.socket.send(parser.encodePayload(packets)); // check periodically if we're done sending this.bufferedAmountId = this.setInterval(function() { - if (socket.bufferedAmount == 0) { + if ('bufferedAmount' in this.socket && this.socket.bufferedAmount == 0) { clearInterval(self.bufferedAmountId); this.writable = true; this.emit('drain'); From 28766dba9e27b1b6159e9800e5afd49b71c7779f Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Sat, 16 Mar 2013 18:33:32 -0400 Subject: [PATCH 04/17] fix featurecheck --- lib/transports/websocket.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/transports/websocket.js b/lib/transports/websocket.js index 36a603663..04c3c4f16 100644 --- a/lib/transports/websocket.js +++ b/lib/transports/websocket.js @@ -88,13 +88,17 @@ WS.prototype.write = function(packets){ this.writable = false; this.socket.send(parser.encodePayload(packets)); // check periodically if we're done sending - this.bufferedAmountId = this.setInterval(function() { - if ('bufferedAmount' in this.socket && this.socket.bufferedAmount == 0) { - clearInterval(self.bufferedAmountId); - this.writable = true; - this.emit('drain'); - } - }, 50); + if ('bufferedAmount' in this.socket) { + this.bufferedAmountId = this.setInterval(function() { + if (this.socket.bufferedAmount == 0) { + clearInterval(self.bufferedAmountId); + this.writable = true; + this.emit('drain'); + } + }, 50); + } else { + // fake drain? + } }; WS.prototype.intervalCleanup = function(){ From c16137a7e5ed0bb571d868137cbcbc2de160ebab Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Sat, 16 Mar 2013 23:09:53 -0400 Subject: [PATCH 05/17] added fake drain --- lib/transports/websocket.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/transports/websocket.js b/lib/transports/websocket.js index 04c3c4f16..692fc61de 100644 --- a/lib/transports/websocket.js +++ b/lib/transports/websocket.js @@ -97,7 +97,9 @@ WS.prototype.write = function(packets){ } }, 50); } else { - // fake drain? + // fake drain + this.writable = true; + this.emit('drain'); } }; From d5fa387a94defa21176baca433642a45af4ce1e3 Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Sat, 16 Mar 2013 23:23:39 -0400 Subject: [PATCH 06/17] wrapped drain call, changed back to encodePacket, moved intervalCleanup to WS, ... --- lib/transports/websocket.js | 38 +++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/lib/transports/websocket.js b/lib/transports/websocket.js index 692fc61de..3b9411274 100644 --- a/lib/transports/websocket.js +++ b/lib/transports/websocket.js @@ -84,29 +84,47 @@ WS.prototype.doOpen = function(){ WS.prototype.write = function(packets){ var self = this; - // encodePayload instead of encodePacket this.writable = false; - this.socket.send(parser.encodePayload(packets)); + // encodePacket efficient as it uses WS framing + // no need for encodePayload + for (var i = 0, l = packets.length; i < l; i++) { + this.socket.send(parser.encodePacket(packets[i])); + } + function ondrain() { + self.writable = true; + self.emit('drain'); + } // check periodically if we're done sending if ('bufferedAmount' in this.socket) { this.bufferedAmountId = this.setInterval(function() { - if (this.socket.bufferedAmount == 0) { + if (self.socket.bufferedAmount == 0) { clearInterval(self.bufferedAmountId); - this.writable = true; - this.emit('drain'); + ondrain(); } }, 50); } else { // fake drain - this.writable = true; - this.emit('drain'); + ondrain(); } }; +/** + * Clear the interval that checks to see if bufferedAmount is 0 + * + * @api private + */ WS.prototype.intervalCleanup = function(){ - if (this.bufferedAmountId) { - clearInterval(this.bufferedAmountId); - } + clearInterval(this.bufferedAmountId); +} + +/** + * Called upon close + * + * @api private + */ +WS.prototype.onClose = function(){ + this.intervalCleanup && this.intervalCleanup.call(this); + Transport.prototype.onClose.call(this); } /** From dab01cf4fbc0a5ad8f41f83409235bdb562d0be2 Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Sat, 16 Mar 2013 23:35:42 -0400 Subject: [PATCH 07/17] removed irrelevant code --- lib/transport.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/transport.js b/lib/transport.js index ed7945978..7eab93b82 100644 --- a/lib/transport.js +++ b/lib/transport.js @@ -90,9 +90,9 @@ Transport.prototype.close = function () { * @api private */ -Transport.prototype.send = function(packets, fn){ +Transport.prototype.send = function(packets){ if ('open' == this.readyState) { - this.write(packets, fn); + this.write(packets); } else { throw new Error('Transport not open'); } @@ -137,6 +137,5 @@ Transport.prototype.onPacket = function (packet) { Transport.prototype.onClose = function () { this.readyState = 'closed'; - this.intervalCleanup && this.intervalCleanup.call(this); this.emit('close'); }; From 05a0647e523c439c7ff29dff26bad67a5f4527ea Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Sat, 16 Mar 2013 16:53:26 -0400 Subject: [PATCH 08/17] add 'drain' emit to WS --- lib/transports/websocket.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/transports/websocket.js b/lib/transports/websocket.js index cb6a566ca..47b3015b9 100644 --- a/lib/transports/websocket.js +++ b/lib/transports/websocket.js @@ -82,9 +82,17 @@ WS.prototype.doOpen = function(){ */ WS.prototype.write = function(packets){ - for (var i = 0, l = packets.length; i < l; i++) { - this.socket.send(parser.encodePacket(packets[i])); - } + // encodePayload instead of encodePacket + this.writable = false; + this.socket.send(parser.encodePayload(packets)); + // check periodically if we're done sending + var bufferedAmountId = this.setInterval(function() { + if (socket.bufferedAmount == 0) { + clearInterval(bufferedAmountId); + this.writable = true; + this.emit('drain'); + } + }, 50); }; /** From 7d941af39bee1fc2fe53495cfd54716bdec5afa8 Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Sat, 16 Mar 2013 17:23:52 -0400 Subject: [PATCH 09/17] make sure to stop interval if transport closes --- lib/transport.js | 5 +++-- lib/transports/websocket.js | 12 ++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/transport.js b/lib/transport.js index 7eab93b82..ed7945978 100644 --- a/lib/transport.js +++ b/lib/transport.js @@ -90,9 +90,9 @@ Transport.prototype.close = function () { * @api private */ -Transport.prototype.send = function(packets){ +Transport.prototype.send = function(packets, fn){ if ('open' == this.readyState) { - this.write(packets); + this.write(packets, fn); } else { throw new Error('Transport not open'); } @@ -137,5 +137,6 @@ Transport.prototype.onPacket = function (packet) { Transport.prototype.onClose = function () { this.readyState = 'closed'; + this.intervalCleanup && this.intervalCleanup.call(this); this.emit('close'); }; diff --git a/lib/transports/websocket.js b/lib/transports/websocket.js index 47b3015b9..338013c3c 100644 --- a/lib/transports/websocket.js +++ b/lib/transports/websocket.js @@ -29,6 +29,7 @@ var global = util.global(); function WS(opts){ Transport.call(this, opts); + this.bufferedAmountId; }; /** @@ -82,19 +83,26 @@ WS.prototype.doOpen = function(){ */ WS.prototype.write = function(packets){ + var self = this; // encodePayload instead of encodePacket this.writable = false; this.socket.send(parser.encodePayload(packets)); // check periodically if we're done sending - var bufferedAmountId = this.setInterval(function() { + this.bufferedAmountId = this.setInterval(function() { if (socket.bufferedAmount == 0) { - clearInterval(bufferedAmountId); + clearInterval(self.bufferedAmountId); this.writable = true; this.emit('drain'); } }, 50); }; +WS.prototype.intervalCleanup = function(){ + if (this.bufferedAmountId) { + clearInterval(this.bufferedAmountId); + } +} + /** * Closes socket. * From e652ead78f09d09c5f97437ffc28bcdbc95cc07d Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Sat, 16 Mar 2013 18:31:58 -0400 Subject: [PATCH 10/17] feature-check for bufferedAmount --- lib/transports/websocket.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/transports/websocket.js b/lib/transports/websocket.js index 338013c3c..36a603663 100644 --- a/lib/transports/websocket.js +++ b/lib/transports/websocket.js @@ -89,7 +89,7 @@ WS.prototype.write = function(packets){ this.socket.send(parser.encodePayload(packets)); // check periodically if we're done sending this.bufferedAmountId = this.setInterval(function() { - if (socket.bufferedAmount == 0) { + if ('bufferedAmount' in this.socket && this.socket.bufferedAmount == 0) { clearInterval(self.bufferedAmountId); this.writable = true; this.emit('drain'); From 08740208eae0ba9c738851631e4f30df65e84318 Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Sat, 16 Mar 2013 18:33:32 -0400 Subject: [PATCH 11/17] fix featurecheck --- lib/transports/websocket.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/transports/websocket.js b/lib/transports/websocket.js index 36a603663..04c3c4f16 100644 --- a/lib/transports/websocket.js +++ b/lib/transports/websocket.js @@ -88,13 +88,17 @@ WS.prototype.write = function(packets){ this.writable = false; this.socket.send(parser.encodePayload(packets)); // check periodically if we're done sending - this.bufferedAmountId = this.setInterval(function() { - if ('bufferedAmount' in this.socket && this.socket.bufferedAmount == 0) { - clearInterval(self.bufferedAmountId); - this.writable = true; - this.emit('drain'); - } - }, 50); + if ('bufferedAmount' in this.socket) { + this.bufferedAmountId = this.setInterval(function() { + if (this.socket.bufferedAmount == 0) { + clearInterval(self.bufferedAmountId); + this.writable = true; + this.emit('drain'); + } + }, 50); + } else { + // fake drain? + } }; WS.prototype.intervalCleanup = function(){ From aa5dec58312489498156ef7e104fb4a9fffd78bb Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Sat, 16 Mar 2013 23:09:53 -0400 Subject: [PATCH 12/17] added fake drain --- lib/transports/websocket.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/transports/websocket.js b/lib/transports/websocket.js index 04c3c4f16..692fc61de 100644 --- a/lib/transports/websocket.js +++ b/lib/transports/websocket.js @@ -97,7 +97,9 @@ WS.prototype.write = function(packets){ } }, 50); } else { - // fake drain? + // fake drain + this.writable = true; + this.emit('drain'); } }; From 74094e179e53369f77ee2b1df2c117ba447a7da4 Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Sat, 16 Mar 2013 23:23:39 -0400 Subject: [PATCH 13/17] wrapped drain call, changed back to encodePacket, moved intervalCleanup to WS, ... --- lib/transports/websocket.js | 38 +++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/lib/transports/websocket.js b/lib/transports/websocket.js index 692fc61de..3b9411274 100644 --- a/lib/transports/websocket.js +++ b/lib/transports/websocket.js @@ -84,29 +84,47 @@ WS.prototype.doOpen = function(){ WS.prototype.write = function(packets){ var self = this; - // encodePayload instead of encodePacket this.writable = false; - this.socket.send(parser.encodePayload(packets)); + // encodePacket efficient as it uses WS framing + // no need for encodePayload + for (var i = 0, l = packets.length; i < l; i++) { + this.socket.send(parser.encodePacket(packets[i])); + } + function ondrain() { + self.writable = true; + self.emit('drain'); + } // check periodically if we're done sending if ('bufferedAmount' in this.socket) { this.bufferedAmountId = this.setInterval(function() { - if (this.socket.bufferedAmount == 0) { + if (self.socket.bufferedAmount == 0) { clearInterval(self.bufferedAmountId); - this.writable = true; - this.emit('drain'); + ondrain(); } }, 50); } else { // fake drain - this.writable = true; - this.emit('drain'); + ondrain(); } }; +/** + * Clear the interval that checks to see if bufferedAmount is 0 + * + * @api private + */ WS.prototype.intervalCleanup = function(){ - if (this.bufferedAmountId) { - clearInterval(this.bufferedAmountId); - } + clearInterval(this.bufferedAmountId); +} + +/** + * Called upon close + * + * @api private + */ +WS.prototype.onClose = function(){ + this.intervalCleanup && this.intervalCleanup.call(this); + Transport.prototype.onClose.call(this); } /** From 71ed0336986481f9a91daf686c06f547508a5930 Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Sat, 16 Mar 2013 23:35:42 -0400 Subject: [PATCH 14/17] removed irrelevant code --- lib/transport.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/transport.js b/lib/transport.js index ed7945978..7eab93b82 100644 --- a/lib/transport.js +++ b/lib/transport.js @@ -90,9 +90,9 @@ Transport.prototype.close = function () { * @api private */ -Transport.prototype.send = function(packets, fn){ +Transport.prototype.send = function(packets){ if ('open' == this.readyState) { - this.write(packets, fn); + this.write(packets); } else { throw new Error('Transport not open'); } @@ -137,6 +137,5 @@ Transport.prototype.onPacket = function (packet) { Transport.prototype.onClose = function () { this.readyState = 'closed'; - this.intervalCleanup && this.intervalCleanup.call(this); this.emit('close'); }; From f3710ff859ca79141ed89b5938a86bb62bcea9cf Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Sun, 17 Mar 2013 00:45:25 -0400 Subject: [PATCH 15/17] fixed websocket flush/drain loop --- lib/transports/websocket.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/transports/websocket.js b/lib/transports/websocket.js index 3b9411274..7f84a49c7 100644 --- a/lib/transports/websocket.js +++ b/lib/transports/websocket.js @@ -104,7 +104,7 @@ WS.prototype.write = function(packets){ }, 50); } else { // fake drain - ondrain(); + setTimeout(ondrain, 0); } }; From 1f6ee883cfe887f17e2e06c3849ac028c6310322 Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Sun, 17 Mar 2013 00:49:50 -0400 Subject: [PATCH 16/17] changes --- lib/transports/websocket.js | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/lib/transports/websocket.js b/lib/transports/websocket.js index 7f84a49c7..e653218ba 100644 --- a/lib/transports/websocket.js +++ b/lib/transports/websocket.js @@ -104,26 +104,19 @@ WS.prototype.write = function(packets){ }, 50); } else { // fake drain + // defer to next tick to allow Socket to clear writeBuffer setTimeout(ondrain, 0); } }; -/** - * Clear the interval that checks to see if bufferedAmount is 0 - * - * @api private - */ -WS.prototype.intervalCleanup = function(){ - clearInterval(this.bufferedAmountId); -} - /** * Called upon close * * @api private */ + WS.prototype.onClose = function(){ - this.intervalCleanup && this.intervalCleanup.call(this); + clearInterval(this.bufferedAmountId); Transport.prototype.onClose.call(this); } From fdb95d579ce6a5f7ab8e47482002e656fbe1c0d1 Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Sun, 17 Mar 2013 00:52:27 -0400 Subject: [PATCH 17/17] changes --- lib/transports/websocket.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/transports/websocket.js b/lib/transports/websocket.js index e653218ba..4ca184d1a 100644 --- a/lib/transports/websocket.js +++ b/lib/transports/websocket.js @@ -29,7 +29,6 @@ var global = util.global(); function WS(opts){ Transport.call(this, opts); - this.bufferedAmountId; }; /** @@ -116,6 +115,7 @@ WS.prototype.write = function(packets){ */ WS.prototype.onClose = function(){ + // stop checking to see if websocket is done sending buffer clearInterval(this.bufferedAmountId); Transport.prototype.onClose.call(this); }