Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions docs/emit.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
16 changes: 14 additions & 2 deletions lib/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down Expand Up @@ -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;
};
16 changes: 15 additions & 1 deletion lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down Expand Up @@ -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.
*
Expand Down