Skip to content

Commit 74893d5

Browse files
[feat] Add a 'binary' flag (#1194)
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 9701611 commit 74893d5

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

docs/API.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
- [socket.emit(eventName[, ...args][, ack])](#socketemiteventname-args-ack)
4242
- [socket.on(eventName, callback)](#socketoneventname-callback)
4343
- [socket.compress(value)](#socketcompressvalue)
44+
- [socket.binary(value)](#socketbinaryvalue)
4445
- [socket.close()](#socketclose)
4546
- [socket.disconnect()](#socketdisconnect)
4647
- [Event: 'connect'](#event-connect)
@@ -526,6 +527,14 @@ Sets a modifier for a subsequent event emission that the event data will only be
526527
socket.compress(false).emit('an event', { some: 'data' });
527528
```
528529

530+
#### socket.binary(value)
531+
532+
Specifies whether the emitted data contains binary. Increases performance when specified. Can be `true` or `false`.
533+
534+
```js
535+
socket.binary(false).emit('an event', { some: 'data' });
536+
```
537+
529538
#### socket.close()
530539

531540
- **Returns** `Socket`

lib/socket.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var on = require('./on');
1010
var bind = require('component-bind');
1111
var debug = require('debug')('socket.io-client:socket');
1212
var parseqs = require('parseqs');
13+
var hasBin = require('has-binary2');
1314

1415
/**
1516
* Module exports.
@@ -62,6 +63,7 @@ function Socket (io, nsp, opts) {
6263
this.sendBuffer = [];
6364
this.connected = false;
6465
this.disconnected = true;
66+
this.flags = {};
6567
if (opts && opts.query) {
6668
this.query = opts.query;
6769
}
@@ -138,7 +140,10 @@ Socket.prototype.emit = function (ev) {
138140
}
139141

140142
var args = toArray(arguments);
141-
var packet = { type: parser.EVENT, data: args };
143+
var packet = {
144+
type: (this.flags.binary !== undefined ? this.flags.binary : hasBin(args)) ? parser.BINARY_EVENT : parser.EVENT,
145+
data: args
146+
};
142147

143148
packet.options = {};
144149
packet.options.compress = !this.flags || false !== this.flags.compress;
@@ -156,7 +161,7 @@ Socket.prototype.emit = function (ev) {
156161
this.sendBuffer.push(packet);
157162
}
158163

159-
delete this.flags;
164+
this.flags = {};
160165

161166
return this;
162167
};
@@ -290,7 +295,7 @@ Socket.prototype.ack = function (id) {
290295
debug('sending ack %j', args);
291296

292297
self.packet({
293-
type: parser.ACK,
298+
type: hasBin(args) ? parser.BINARY_ACK : parser.ACK,
294299
id: id,
295300
data: args
296301
});
@@ -412,7 +417,19 @@ Socket.prototype.disconnect = function () {
412417
*/
413418

414419
Socket.prototype.compress = function (compress) {
415-
this.flags = this.flags || {};
416420
this.flags.compress = compress;
417421
return this;
418422
};
423+
424+
/**
425+
* Sets the binary flag
426+
*
427+
* @param {Boolean} whether the emitted data contains binary
428+
* @return {Socket} self
429+
* @api public
430+
*/
431+
432+
Socket.prototype.binary = function (binary) {
433+
this.flags.binary = binary;
434+
return this;
435+
};

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@
2121
"component-emitter": "1.2.1",
2222
"debug": "~3.1.0",
2323
"engine.io-client": "~3.2.0",
24+
"has-binary2": "~1.0.2",
2425
"has-cors": "1.1.0",
2526
"indexof": "0.0.1",
2627
"object-component": "0.0.3",
2728
"parseqs": "0.0.5",
2829
"parseuri": "0.0.5",
29-
"socket.io-parser": "3.1.2",
30+
"socket.io-parser": "~3.2.0",
3031
"to-array": "0.1.4"
3132
},
3233
"devDependencies": {
@@ -48,7 +49,7 @@
4849
"imports-loader": "^0.7.1",
4950
"istanbul": "^0.4.5",
5051
"mocha": "^3.3.0",
51-
"socket.io": "2.0.4",
52+
"socket.io": "socketio/socket.io",
5253
"socket.io-browsers": "^1.0.0",
5354
"strip-loader": "0.1.2",
5455
"text-blob-builder": "0.0.1",

0 commit comments

Comments
 (0)