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); }; /**