Implemented Socket buffering/flushing:

- Fires upon upgrade
- Fires when the transport is drained.
This commit is contained in:
Guillermo Rauch
2012-01-14 09:34:29 -08:00
parent fba2a89b79
commit 83c2711009

View File

@@ -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.
*