From 05a0647e523c439c7ff29dff26bad67a5f4527ea Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Sat, 16 Mar 2013 16:53:26 -0400 Subject: [PATCH] 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 cb6a566c..47b3015b 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); }; /**