Skip to content

Commit

Permalink
[feat] Add a 'binary' flag (#1194)
Browse files Browse the repository at this point in the history
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);
```
  • Loading branch information
darrachequesne authored Mar 10, 2018
1 parent 9701611 commit 74893d5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
9 changes: 9 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
- [socket.emit(eventName[, ...args][, ack])](#socketemiteventname-args-ack)
- [socket.on(eventName, callback)](#socketoneventname-callback)
- [socket.compress(value)](#socketcompressvalue)
- [socket.binary(value)](#socketbinaryvalue)
- [socket.close()](#socketclose)
- [socket.disconnect()](#socketdisconnect)
- [Event: 'connect'](#event-connect)
Expand Down Expand Up @@ -526,6 +527,14 @@ Sets a modifier for a subsequent event emission that the event data will only be
socket.compress(false).emit('an event', { some: 'data' });
```

#### socket.binary(value)

Specifies whether the emitted data contains binary. Increases performance when specified. Can be `true` or `false`.

```js
socket.binary(false).emit('an event', { some: 'data' });
```

#### socket.close()

- **Returns** `Socket`
Expand Down
25 changes: 21 additions & 4 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var on = require('./on');
var bind = require('component-bind');
var debug = require('debug')('socket.io-client:socket');
var parseqs = require('parseqs');
var hasBin = require('has-binary2');

/**
* Module exports.
Expand Down Expand Up @@ -62,6 +63,7 @@ function Socket (io, nsp, opts) {
this.sendBuffer = [];
this.connected = false;
this.disconnected = true;
this.flags = {};
if (opts && opts.query) {
this.query = opts.query;
}
Expand Down Expand Up @@ -138,7 +140,10 @@ Socket.prototype.emit = function (ev) {
}

var args = toArray(arguments);
var packet = { type: parser.EVENT, data: args };
var packet = {
type: (this.flags.binary !== undefined ? this.flags.binary : hasBin(args)) ? parser.BINARY_EVENT : parser.EVENT,
data: args
};

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

delete this.flags;
this.flags = {};

return this;
};
Expand Down Expand Up @@ -290,7 +295,7 @@ Socket.prototype.ack = function (id) {
debug('sending ack %j', args);

self.packet({
type: parser.ACK,
type: hasBin(args) ? parser.BINARY_ACK : parser.ACK,
id: id,
data: args
});
Expand Down Expand Up @@ -412,7 +417,19 @@ Socket.prototype.disconnect = function () {
*/

Socket.prototype.compress = function (compress) {
this.flags = this.flags || {};
this.flags.compress = compress;
return this;
};

/**
* Sets the binary flag
*
* @param {Boolean} whether the emitted data contains binary
* @return {Socket} self
* @api public
*/

Socket.prototype.binary = function (binary) {
this.flags.binary = binary;
return this;
};
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
"component-emitter": "1.2.1",
"debug": "~3.1.0",
"engine.io-client": "~3.2.0",
"has-binary2": "~1.0.2",
"has-cors": "1.1.0",
"indexof": "0.0.1",
"object-component": "0.0.3",
"parseqs": "0.0.5",
"parseuri": "0.0.5",
"socket.io-parser": "3.1.2",
"socket.io-parser": "~3.2.0",
"to-array": "0.1.4"
},
"devDependencies": {
Expand All @@ -48,7 +49,7 @@
"imports-loader": "^0.7.1",
"istanbul": "^0.4.5",
"mocha": "^3.3.0",
"socket.io": "2.0.4",
"socket.io": "socketio/socket.io",
"socket.io-browsers": "^1.0.0",
"strip-loader": "0.1.2",
"text-blob-builder": "0.0.1",
Expand Down

0 comments on commit 74893d5

Please sign in to comment.