From 7eccc4d9ae4028e92555672cc6ae1a585c67d994 Mon Sep 17 00:00:00 2001 From: Andrew S Date: Wed, 25 Jan 2017 19:55:37 -0500 Subject: [PATCH 1/9] Add the hasBinary flag --- lib/socket.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/socket.js b/lib/socket.js index e97536f214..4f4d4c97eb 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -141,9 +141,17 @@ Socket.prototype.emit = function(ev){ if (~exports.events.indexOf(ev)) { emit.apply(this, arguments); } else { - var args = Array.prototype.slice.call(arguments); + var args = Array.prototype.slice.call(arguments), + type = parser.EVENT; + + if (this.flags.hasBinary === undefined) { + hasBin(args) && (type = parser.BINARY_EVENT); + } else { + this.flags.hasBinary && (type = parser.BINARY_EVENT); + } + var packet = { - type: hasBin(args) ? parser.BINARY_EVENT : parser.EVENT, + type: type, data: args }; @@ -481,6 +489,20 @@ Socket.prototype.compress = function(compress){ return this; }; +/** + * Sets the hasBinary flag + * + * @param {Boolean} Encode as if it has binary data if `true`, Encode as if it doesnt have binary data if `false` + * @return {Socket} self + * @api public + */ + + Socket.prototype.hasBinary = function (bool) { + this.flags = this.flags || {}; + this.flags.hasBinary = bool; + return this; + }; + /** * Dispatch incoming event to socket listeners. * From ae0b325203f05f00830e95d6f944b2fa6c547ac8 Mon Sep 17 00:00:00 2001 From: Andrew S Date: Wed, 25 Jan 2017 20:13:43 -0500 Subject: [PATCH 2/9] Dont need this anymore --- lib/socket.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/socket.js b/lib/socket.js index 4f4d4c97eb..80a5d5ea16 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -498,7 +498,6 @@ Socket.prototype.compress = function(compress){ */ Socket.prototype.hasBinary = function (bool) { - this.flags = this.flags || {}; this.flags.hasBinary = bool; return this; }; From b0acd595336b4f9cb46fb94009a655812f6fa7a6 Mon Sep 17 00:00:00 2001 From: Andrew S Date: Fri, 27 Jan 2017 08:03:51 -0500 Subject: [PATCH 3/9] Cleanup --- lib/socket.js | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/lib/socket.js b/lib/socket.js index 80a5d5ea16..7a9f92f72e 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -141,17 +141,10 @@ Socket.prototype.emit = function(ev){ if (~exports.events.indexOf(ev)) { emit.apply(this, arguments); } else { - var args = Array.prototype.slice.call(arguments), - type = parser.EVENT; - - if (this.flags.hasBinary === undefined) { - hasBin(args) && (type = parser.BINARY_EVENT); - } else { - this.flags.hasBinary && (type = parser.BINARY_EVENT); - } + var args = Array.prototype.slice.call(arguments); var packet = { - type: type, + type: (this.flags.hasBinary !== undefined ? this.flags.hasBinary : hasBin(args)) ? parser.BINARY_EVENT : parser.EVENT, data: args }; From 24e13166c05d3afbdd713caeb7237315140e181f Mon Sep 17 00:00:00 2001 From: Andrew S Date: Fri, 27 Jan 2017 08:07:44 -0500 Subject: [PATCH 4/9] Add to namespace --- lib/namespace.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/namespace.js b/lib/namespace.js index 186649dc1c..d11da5e81a 100644 --- a/lib/namespace.js +++ b/lib/namespace.js @@ -210,8 +210,7 @@ Namespace.prototype.emit = function(ev){ } else { // set up packet object var args = Array.prototype.slice.call(arguments); - var parserType = parser.EVENT; // default - if (hasBin(args)) { parserType = parser.BINARY_EVENT; } // binary + var parserType = (this.flags.hasBinary !== undefined ? this.flags.hasBinary : hasBin(args)) ? parser.BINARY_EVENT : parser.EVENT; var packet = { type: parserType, data: args }; @@ -272,3 +271,16 @@ Namespace.prototype.compress = function(compress){ this.flags.compress = compress; return this; }; + +/** + * Sets the hasBinary flag + * + * @param {Boolean} Encode as if it has binary data if `true`, Encode as if it doesnt have binary data if `false` + * @return {Socket} self + * @api public + */ + + Namespace.prototype.hasBinary = function (bool) { + this.flags.hasBinary = bool; + return this; + }; From fb702bb4548690fcd88547690d9ffba007ec1eb4 Mon Sep 17 00:00:00 2001 From: Andrew S Date: Fri, 27 Jan 2017 08:12:17 -0500 Subject: [PATCH 5/9] Update documentation --- docs/emit.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/emit.md b/docs/emit.md index 344d1b380f..c7d1a6c03b 100644 --- a/docs/emit.md +++ b/docs/emit.md @@ -37,6 +37,9 @@ function onConnect(socket){ // sending a message that might be dropped if the client is not ready to receive messages socket.volatile.emit('maybe', 'do you really need it?'); + // specifying whether the data to send has binary data + socket.hasBinary(false).emit('what', 'I have no binarys!'); + // sending to all clients on this node (when using multiple nodes) io.local.emit('hi', 'my lovely babies'); From c84cc09f6c0ccce3b89c597b9ad4fdb0e36739b3 Mon Sep 17 00:00:00 2001 From: Andrew S Date: Fri, 27 Jan 2017 08:23:07 -0500 Subject: [PATCH 6/9] Documentation --- docs/API.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/docs/API.md b/docs/API.md index 97b40d96c8..e2ef67cbc7 100644 --- a/docs/API.md +++ b/docs/API.md @@ -29,6 +29,7 @@ - [Event: 'connection'](#event-connect) - [Flag: 'volatile'](#flag-volatile) - [Flag: 'local'](#flag-local) + - [Flag: 'hasBinary'](#flag-hasBinary) - [Class: Socket](#socket) - [socket.id](#socketid) - [socket.rooms](#socketrooms) @@ -47,6 +48,7 @@ - [socket.disconnect(close)](#socketdisconnectclose) - [Flag: 'broadcast'](#flag-broadcast) - [Flag: 'volatile'](#flag-volatile-1) + - [Flag: 'hasBinary'](#flag-hasBinary-1) - [Event: 'disconnect'](#event-disconnect) - [Event: 'error'](#event-error) - [Event: 'disconnecting'](#event-disconnecting) @@ -327,6 +329,14 @@ Sets a modifier for a subsequent event emission that the event data may be lost io.volatile.emit('an event', { some: 'data' }); // the clients may or may not receive it ``` +#### Flag: 'hasBinary' + +Specifies whether there is binary data in the data to send. Increases preformance when specified. Can be `true` or `false`. + +```js +io.hasBinary(false).emit('an event', { some: 'data' }); +``` + #### Flag: 'local' Sets a modifier for a subsequent event emission that the event data will only be _broadcast_ to the current node (when the [Redis adapter](https://github.com/socketio/socket.io-redis) is used). @@ -545,6 +555,17 @@ io.on('connection', function(socket){ }); ``` +#### Flag: 'hasBinary' + +Specifies whether there is binary data in the data to send. Increases preformance when specified. Can be `true` or `false`. + +```js +var io = require('socket.io')(); +io.on('connection', function(socket){ + socket.hasBinary(false).emit('an event', { some: 'data' }); // The data to send has no binary data +}); +``` + #### Event: 'disconnect' - `reason` _(String)_ the reason of the disconnection (either client or server-side) From d27ab74c8b979b851b821a78450cf623e642c4fb Mon Sep 17 00:00:00 2001 From: Andrew S Date: Fri, 27 Jan 2017 08:27:28 -0500 Subject: [PATCH 7/9] Link fixes --- docs/API.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/API.md b/docs/API.md index e2ef67cbc7..792016e951 100644 --- a/docs/API.md +++ b/docs/API.md @@ -29,7 +29,7 @@ - [Event: 'connection'](#event-connect) - [Flag: 'volatile'](#flag-volatile) - [Flag: 'local'](#flag-local) - - [Flag: 'hasBinary'](#flag-hasBinary) + - [Flag: 'hasBinary'](#flag-hasbinary) - [Class: Socket](#socket) - [socket.id](#socketid) - [socket.rooms](#socketrooms) @@ -48,7 +48,7 @@ - [socket.disconnect(close)](#socketdisconnectclose) - [Flag: 'broadcast'](#flag-broadcast) - [Flag: 'volatile'](#flag-volatile-1) - - [Flag: 'hasBinary'](#flag-hasBinary-1) + - [Flag: 'hasBinary'](#flag-hasbinary-1) - [Event: 'disconnect'](#event-disconnect) - [Event: 'error'](#event-error) - [Event: 'disconnecting'](#event-disconnecting) From e99224b22b0b5805d0ea9d7e97058b4467b193eb Mon Sep 17 00:00:00 2001 From: Andrew S Date: Fri, 27 Jan 2017 08:29:37 -0500 Subject: [PATCH 8/9] Spelling --- docs/emit.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/emit.md b/docs/emit.md index c7d1a6c03b..fb5b3be4bc 100644 --- a/docs/emit.md +++ b/docs/emit.md @@ -38,7 +38,7 @@ function onConnect(socket){ socket.volatile.emit('maybe', 'do you really need it?'); // specifying whether the data to send has binary data - socket.hasBinary(false).emit('what', 'I have no binarys!'); + socket.hasBinary(false).emit('what', 'I have no binaries!'); // sending to all clients on this node (when using multiple nodes) io.local.emit('hi', 'my lovely babies'); From dfcee4c32fe35bc37e5c2a4ad7fbc61cdb9bc398 Mon Sep 17 00:00:00 2001 From: Andrew S Date: Fri, 27 Jan 2017 12:49:07 -0500 Subject: [PATCH 9/9] Spelling --- docs/API.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/API.md b/docs/API.md index 792016e951..3dafd0f436 100644 --- a/docs/API.md +++ b/docs/API.md @@ -331,7 +331,7 @@ io.volatile.emit('an event', { some: 'data' }); // the clients may or may not re #### Flag: 'hasBinary' -Specifies whether there is binary data in the data to send. Increases preformance when specified. Can be `true` or `false`. +Specifies whether there is binary data in the emitted data. Increases performance when specified. Can be `true` or `false`. ```js io.hasBinary(false).emit('an event', { some: 'data' }); @@ -557,7 +557,7 @@ io.on('connection', function(socket){ #### Flag: 'hasBinary' -Specifies whether there is binary data in the data to send. Increases preformance when specified. Can be `true` or `false`. +Specifies whether there is binary data in the emmited data. Increases performance when specified. Can be `true` or `false`. ```js var io = require('socket.io')();