Skip to content

Commit f48a06c

Browse files
authoredFeb 28, 2018
[feat] Add a 'binary' flag (#3185)
So that the call to the `has-binary` method can be skipped. Usage: ``` // with binary data socket.binary(true).emit("binary", obj); // without binary data socket.binary(false).emit("string", obj); // call to hasBin socket.emit("guess", obj); ```
1 parent 0539a2c commit f48a06c

File tree

6 files changed

+62
-6
lines changed

6 files changed

+62
-6
lines changed
 

‎docs/API.md

+21
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
- [Event: 'connection'](#event-connect)
3434
- [Flag: 'volatile'](#flag-volatile)
3535
- [Flag: 'local'](#flag-local)
36+
- [Flag: 'binary'](#flag-binary)
3637
- [Class: Socket](#socket)
3738
- [socket.id](#socketid)
3839
- [socket.rooms](#socketrooms)
@@ -57,6 +58,7 @@
5758
- [socket.disconnect(close)](#socketdisconnectclose)
5859
- [Flag: 'broadcast'](#flag-broadcast)
5960
- [Flag: 'volatile'](#flag-volatile-1)
61+
- [Flag: 'binary'](#flag-binary-1)
6062
- [Event: 'disconnect'](#event-disconnect)
6163
- [Event: 'error'](#event-error)
6264
- [Event: 'disconnecting'](#event-disconnecting)
@@ -470,6 +472,14 @@ Sets a modifier for a subsequent event emission that the event data may be lost
470472
io.volatile.emit('an event', { some: 'data' }); // the clients may or may not receive it
471473
```
472474

475+
#### Flag: 'binary'
476+
477+
Specifies whether there is binary data in the emitted data. Increases performance when specified. Can be `true` or `false`.
478+
479+
```js
480+
io.binary(false).emit('an event', { some: 'data' });
481+
```
482+
473483
#### Flag: 'local'
474484

475485
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).
@@ -769,6 +779,17 @@ io.on('connection', (socket) => {
769779
});
770780
```
771781

782+
#### Flag: 'binary'
783+
784+
Specifies whether there is binary data in the emitted data. Increases performance when specified. Can be `true` or `false`.
785+
786+
```js
787+
var io = require('socket.io')();
788+
io.on('connection', function(socket){
789+
socket.binary(false).emit('an event', { some: 'data' }); // The data to send has no binary data
790+
});
791+
```
792+
772793
#### Event: 'disconnect'
773794

774795
- `reason` _(String)_ the reason of the disconnection (either client or server-side)

‎docs/emit.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ function onConnect(socket){
4040
// sending a message that might be dropped if the client is not ready to receive messages
4141
socket.volatile.emit('maybe', 'do you really need it?');
4242

43+
// specifying whether the data to send has binary data
44+
socket.binary(false).emit('what', 'I have no binaries!');
45+
4346
// sending to all clients on this node (when using multiple nodes)
4447
io.local.emit('hi', 'my lovely babies');
45-
48+
4649
// sending to all connected clients
4750
io.emit('an event sent to all connected clients');
4851

‎lib/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ var emitterMethods = Object.keys(Emitter.prototype).filter(function(key){
451451
return typeof Emitter.prototype[key] === 'function';
452452
});
453453

454-
emitterMethods.concat(['to', 'in', 'use', 'send', 'write', 'clients', 'compress']).forEach(function(fn){
454+
emitterMethods.concat(['to', 'in', 'use', 'send', 'write', 'clients', 'compress', 'binary']).forEach(function(fn){
455455
Server.prototype[fn] = function(){
456456
return this.sockets[fn].apply(this.sockets, arguments);
457457
};

‎lib/namespace.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
var Socket = require('./socket');
77
var Emitter = require('events').EventEmitter;
88
var parser = require('socket.io-parser');
9+
var hasBin = require('has-binary2');
910
var debug = require('debug')('socket.io:namespace');
1011

1112
/**
@@ -214,7 +215,10 @@ Namespace.prototype.emit = function(ev){
214215
}
215216
// set up packet object
216217
var args = Array.prototype.slice.call(arguments);
217-
var packet = { type: parser.EVENT, data: args };
218+
var packet = {
219+
type: (this.flags.binary !== undefined ? this.flags.binary : hasBin(args)) ? parser.BINARY_EVENT : parser.EVENT,
220+
data: args
221+
};
218222

219223
if ('function' == typeof args[args.length - 1]) {
220224
throw new Error('Callbacks are not supported when broadcasting');
@@ -277,3 +281,16 @@ Namespace.prototype.compress = function(compress){
277281
this.flags.compress = compress;
278282
return this;
279283
};
284+
285+
/**
286+
* Sets the binary flag
287+
*
288+
* @param {Boolean} Encode as if it has binary data if `true`, Encode as if it doesnt have binary data if `false`
289+
* @return {Socket} self
290+
* @api public
291+
*/
292+
293+
Namespace.prototype.binary = function (binary) {
294+
this.flags.binary = binary;
295+
return this;
296+
};

‎lib/socket.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
var Emitter = require('events').EventEmitter;
77
var parser = require('socket.io-parser');
8+
var hasBin = require('has-binary2');
89
var url = require('url');
910
var debug = require('debug')('socket.io:socket');
1011

@@ -143,7 +144,7 @@ Socket.prototype.emit = function(ev){
143144

144145
var args = Array.prototype.slice.call(arguments);
145146
var packet = {
146-
type: parser.EVENT,
147+
type: (this.flags.binary !== undefined ? this.flags.binary : hasBin(args)) ? parser.BINARY_EVENT : parser.EVENT,
147148
data: args
148149
};
149150

@@ -380,7 +381,7 @@ Socket.prototype.ack = function(id){
380381

381382
self.packet({
382383
id: id,
383-
type: parser.ACK,
384+
type: hasBin(args) ? parser.BINARY_ACK : parser.ACK,
384385
data: args
385386
});
386387

@@ -495,6 +496,19 @@ Socket.prototype.compress = function(compress){
495496
return this;
496497
};
497498

499+
/**
500+
* Sets the binary flag
501+
*
502+
* @param {Boolean} Encode as if it has binary data if `true`, Encode as if it doesnt have binary data if `false`
503+
* @return {Socket} self
504+
* @api public
505+
*/
506+
507+
Socket.prototype.binary = function (binary) {
508+
this.flags.binary = binary;
509+
return this;
510+
};
511+
498512
/**
499513
* Dispatch incoming event to socket listeners.
500514
*

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
"dependencies": {
2727
"debug": "~3.1.0",
2828
"engine.io": "~3.1.0",
29+
"has-binary2": "~1.0.2",
2930
"socket.io-adapter": "~1.1.0",
3031
"socket.io-client": "2.0.4",
31-
"socket.io-parser": "~3.1.1"
32+
"socket.io-parser": "~3.2.0"
3233
},
3334
"devDependencies": {
3435
"expect.js": "0.3.1",

0 commit comments

Comments
 (0)
Please sign in to comment.