support permessage-deflate

This commit is contained in:
Naoyuki Kanezawa
2015-01-06 03:20:08 +09:00
parent e352dbf834
commit f6bf9f807f
4 changed files with 37 additions and 11 deletions

View File

@@ -199,11 +199,16 @@ Exposed as `eio` in the browser standalone build.
- `ca` (`String`|`Array`): An authority certificate or array of authority certificates to check the remote host against.. Can be used in Node.js client environment to manually specify certificate information.
- `ciphers` (`String`): A string describing the ciphers to use or exclude. Consult the [cipher format list](http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT) for details on the format.. Can be used in Node.js client environment to manually specify certificate information.
- `rejectUnauthorized` (`Boolean`): If true, the server certificate is verified against the list of supplied CAs. An 'error' event is emitted if verification fails. Verification happens at the connection level, before the HTTP request is sent. Can be used in Node.js client environment to manually specify certificate information.
- `perMessageDeflate` (`Object|Boolean`): parameters of the WebSocket permessage-deflate extension
(see [ws module](https://github.com/einaros/ws) api docs). Set to `false` to disable. (`true`)
- `send`
- Sends a message to the server
- **Parameters**
- `String` | `ArrayBuffer` | `ArrayBufferView` | `Blob`: data to send
- `Object`: optional, options object
- `Function`: optional, callback upon `drain`
- **Options**
- `compress` (`Boolean`): whether to compress sending data. This option is ignored and forced to be `true` on the browser. (`true`)
- `close`
- Disconnects the client.

View File

@@ -84,6 +84,7 @@ function Socket(uri, opts){
this.rememberUpgrade = opts.rememberUpgrade || false;
this.binaryType = null;
this.onlyBinaryUpgrades = opts.onlyBinaryUpgrades;
this.perMessageDeflate = false !== opts.perMessageDeflate ? (opts.perMessageDeflate || true) : false;
// SSL options for Node.js client
this.pfx = opts.pfx || null;
@@ -165,7 +166,8 @@ Socket.prototype.createTransport = function (name) {
cert: this.cert,
ca: this.ca,
ciphers: this.ciphers,
rejectUnauthorized: this.rejectUnauthorized
rejectUnauthorized: this.rejectUnauthorized,
perMessageDeflate: this.perMessageDeflate
});
return transport;
@@ -273,7 +275,7 @@ Socket.prototype.probe = function (name) {
if (failed) return;
debug('probe transport "%s" opened', name);
transport.send([{ type: 'ping', data: 'probe' }]);
transport.send([{ type: 'ping', data: 'probe', options: { compress: true } }]);
transport.once('packet', function (msg) {
if (failed) return;
if ('pong' == msg.type && 'probe' == msg.data) {
@@ -292,7 +294,7 @@ Socket.prototype.probe = function (name) {
cleanup();
self.setTransport(transport);
transport.send([{ type: 'upgrade' }]);
transport.send([{ type: 'upgrade', options: { compress: true } }]);
self.emit('upgrade', transport);
transport = null;
self.upgrading = false;
@@ -548,13 +550,14 @@ Socket.prototype.flush = function () {
*
* @param {String} message.
* @param {Function} callback function.
* @param {Object} options.
* @return {Socket} for chaining.
* @api public
*/
Socket.prototype.write =
Socket.prototype.send = function (msg, fn) {
this.sendPacket('message', msg, fn);
Socket.prototype.send = function (msg, options, fn) {
this.sendPacket('message', msg, options, fn);
return this;
};
@@ -563,16 +566,29 @@ Socket.prototype.send = function (msg, fn) {
*
* @param {String} packet type.
* @param {String} data.
* @param {Object} options.
* @param {Function} callback function.
* @api private
*/
Socket.prototype.sendPacket = function (type, data, fn) {
Socket.prototype.sendPacket = function (type, data, options, fn) {
if ('function' == typeof options) {
fn = options;
options = null;
}
if ('closing' == this.readyState || 'closed' == this.readyState) {
return;
}
var packet = { type: type, data: data };
options = options || {};
options.compress = false !== options.compress;
var packet = {
type: type,
data: data,
options: options
};
this.emit('packetCreate', packet);
this.writeBuffer.push(packet);
this.callbackBuffer.push(fn);

View File

@@ -34,6 +34,7 @@ function WS(opts){
if (forceBase64) {
this.supportsBinary = false;
}
this.perMessageDeflate = opts.perMessageDeflate;
Transport.call(this, opts);
}
@@ -72,7 +73,10 @@ WS.prototype.doOpen = function(){
var self = this;
var uri = this.uri();
var protocols = void(0);
var opts = { agent: this.agent };
var opts = {
agent: this.agent,
perMessageDeflate: this.perMessageDeflate
};
// SSL options for Node.js client
opts.pfx = this.pfx;
@@ -146,12 +150,13 @@ WS.prototype.write = function(packets){
// encodePacket efficient as it uses WS framing
// no need for encodePayload
for (var i = 0, l = packets.length; i < l; i++) {
parser.encodePacket(packets[i], this.supportsBinary, function(data) {
var packet = packets[i];
parser.encodePacket(packet, this.supportsBinary, function(data) {
//Sometimes the websocket has already been closed but the browser didn't
//have a chance of informing us about it yet, in that case send will
//throw an error
try {
self.ws.send(data);
self.ws.send(data, packet.options);
} catch (e){
debug('websocket closed before onclose event');
}

View File

@@ -23,7 +23,7 @@
],
"dependencies": {
"has-cors": "1.0.3",
"ws": "0.4.31",
"ws": "0.6.5",
"xmlhttprequest": "rase-/node-XMLHttpRequest#a6b6f2",
"component-emitter": "1.1.2",
"indexof": "0.0.1",