From 83c27110093df14e4325f66cfa8f7ea800bbc31b Mon Sep 17 00:00:00 2001 From: Guillermo Rauch Date: Sat, 14 Jan 2012 09:34:29 -0800 Subject: [PATCH] Implemented Socket buffering/flushing: - Fires upon upgrade - Fires when the transport is drained. --- lib/socket.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/socket.js b/lib/socket.js index da6cc543..40f7834c 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -122,6 +122,7 @@ Socket.prototype.setTransport = function (transport) { this.transport = transport; this.transport.once('error', this.onError.bind(this)); this.transport.on('packet', this.onPacket.bind(this)); + this.transport.on('drain', this.flush.bind(this)); this.transport.once('close', this.onClose.bind(this, 'transport close')); }; @@ -141,6 +142,7 @@ Socket.prototype.upgrade = function (transport) { // therefore we don't worry about the `open` event this.setTransport(transport); this.ping(); + this.flush(); }; /** @@ -196,6 +198,20 @@ Socket.prototype.sendPacket = function (type, data) { } }; +/** + * Attempts to flush the packets buffer. + * + * @api private + */ + +Socket.prototype.flush = function () { + if ('close' != this.readyState && this.transport.writable) { + debug('flushing buffer to transport'); + this.transport.send(this.writeBuffer); + this.writeBuffer = []; + } +}; + /** * Closes the socket and underlying transport. *