diff --git a/docs/API.md b/docs/API.md index 97b40d96c8..3dafd0f436 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 emitted data. Increases performance 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 emmited data. Increases performance 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) diff --git a/docs/emit.md b/docs/emit.md index 344d1b380f..fb5b3be4bc 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 binaries!'); + // sending to all clients on this node (when using multiple nodes) io.local.emit('hi', 'my lovely babies'); 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; + }; diff --git a/lib/socket.js b/lib/socket.js index e97536f214..7a9f92f72e 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -142,8 +142,9 @@ Socket.prototype.emit = function(ev){ emit.apply(this, arguments); } else { var args = Array.prototype.slice.call(arguments); + var packet = { - type: hasBin(args) ? parser.BINARY_EVENT : parser.EVENT, + type: (this.flags.hasBinary !== undefined ? this.flags.hasBinary : hasBin(args)) ? parser.BINARY_EVENT : parser.EVENT, data: args }; @@ -481,6 +482,19 @@ 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.hasBinary = bool; + return this; + }; + /** * Dispatch incoming event to socket listeners. *