From cf965a131d80aed6fcbbf3100f62927dc6ebbd48 Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Wed, 5 Jan 2011 12:17:27 +0100 Subject: [PATCH] ending a response when the last write was not flushed might result in incomplete sends -> wait for the drain event before ending the request. --- lib/socket.io/transports/xhr-polling.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/socket.io/transports/xhr-polling.js b/lib/socket.io/transports/xhr-polling.js index 112534c741..e98f73c2ae 100644 --- a/lib/socket.io/transports/xhr-polling.js +++ b/lib/socket.io/transports/xhr-polling.js @@ -72,8 +72,16 @@ Polling.prototype._write = function(message){ if (this.request.headers.cookie) headers['Access-Control-Allow-Credentials'] = 'true'; } this.response.writeHead(200, headers); - this.response.write(message); - this.response.end(); - this._onClose(); + var flushed = this.response.write(message); + if (flushed) { + this.response.end(); + this._onClose(); + } else { + var self = this; + this.response.connection.on("drain", function() { + self.response.end(); + self._onClose(); + }); + } } };