Skip to content

Commit

Permalink
Merge pull request #951 from websockets/use/boolean
Browse files Browse the repository at this point in the history
Use a boolean instead of an options object
  • Loading branch information
3rd-Eden committed Jan 3, 2017
2 parents aabbffc + 338327c commit b187775
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 53 deletions.
22 changes: 10 additions & 12 deletions doc/ws.md
Expand Up @@ -320,25 +320,23 @@ receives an `OpenEvent` named "open".

Pause the socket.

### websocket.ping([data[, options[, dontFailWhenClosed]]])
### websocket.ping([data[, mask[, failSilently]]])

- `data` {Any} The data to send in the ping frame.
- `options` {Object}
- `mask` {Boolean} Specifies whether `data` should be masked or not. Defaults
to `true` when `websocket` is not a server client.
- `dontFailWhenClosed` {Boolean} Specifies whether or not to throw an error if
the connection is not open.
- `mask` {Boolean} Specifies whether `data` should be masked or not. Defaults
to `true` when `websocket` is not a server client.
- `failSilently` {Boolean} Specifies whether or not to throw an error if the
connection is not open.

Send a ping.

### websocket.pong([data[, options[, dontFailWhenClosed]]])
### websocket.pong([data[, mask[, failSilently]]])

- `data` {Any} The data to send in the ping frame.
- `options` {Object}
- `mask` {Boolean} Specifies whether `data` should be masked or not. Defaults
to `true` when `websocket` is not a server client.
- `dontFailWhenClosed` {Boolean} Specifies whether or not to throw an error if
the connection is not open.
- `mask` {Boolean} Specifies whether `data` should be masked or not. Defaults
to `true` when `websocket` is not a server client.
- `failSilently` {Boolean} Specifies whether or not to throw an error if the
connection is not open.

Send a pong.

Expand Down
22 changes: 9 additions & 13 deletions lib/Sender.js
Expand Up @@ -12,8 +12,6 @@ const PerMessageDeflate = require('./PerMessageDeflate');
const bufferUtil = require('./BufferUtil').BufferUtil;
const ErrorCodes = require('./ErrorCodes');

const noop = () => {};

/**
* HyBi Sender implementation.
*/
Expand All @@ -30,7 +28,7 @@ class Sender {
this.processing = false;
this.compress = false;
this._socket = socket;
this.onerror = noop;
this.onerror = null;
this.queue = [];
}

Expand Down Expand Up @@ -84,15 +82,14 @@ class Sender {
* Sends a ping message to the other peer.
*
* @param {*} data The message to send
* @param {Object} options Options object
* @param {Boolean} options.mask Specifies whether or not to mask `data`
* @param {Boolean} mask Specifies whether or not to mask `data`
* @public
*/
ping (data, options) {
ping (data, mask) {
if (this.perMessageDeflate) {
this.enqueue([this.doPing, data, options.mask]);
this.enqueue([this.doPing, data, mask]);
} else {
this.doPing(data, options.mask);
this.doPing(data, mask);
}
}

Expand All @@ -119,15 +116,14 @@ class Sender {
* Sends a pong message to the other peer.
*
* @param {*} data The message to send
* @param {Object} options Options object
* @param {Boolean} options.mask Specifies whether or not to mask `data`
* @param {Boolean} mask Specifies whether or not to mask `data`
* @public
*/
pong (data, options) {
pong (data, mask) {
if (this.perMessageDeflate) {
this.enqueue([this.doPong, data, options.mask]);
this.enqueue([this.doPong, data, mask]);
} else {
this.doPong(data, options.mask);
this.doPong(data, mask);
}
}

Expand Down
32 changes: 13 additions & 19 deletions lib/WebSocket.js
Expand Up @@ -140,7 +140,7 @@ class WebSocket extends EventEmitter {
// receiver event handlers
this._receiver.onmessage = (data, flags) => this.emit('message', data, flags);
this._receiver.onping = (data, flags) => {
this.pong(data, { mask: !this._isServer }, true);
this.pong(data, !this._isServer, true);
this.emit('ping', data, flags);
};
this._receiver.onpong = (data, flags) => this.emit('pong', data, flags);
Expand Down Expand Up @@ -294,42 +294,36 @@ class WebSocket extends EventEmitter {
* Send a ping message.
*
* @param {*} data The message to send
* @param {Object} options Options object
* @param {Boolean} options.mask Indicates whether or not to mask `data`
* @param {Boolean} dontFailWhenClosed Indicates whether or not to throw an if the connection isn't open
* @param {Boolean} mask Indicates whether or not to mask `data`
* @param {Boolean} failSilently Indicates whether or not to throw if `readyState` isn't `OPEN`
* @public
*/
ping (data, options, dontFailWhenClosed) {
ping (data, mask, failSilently) {
if (this.readyState !== WebSocket.OPEN) {
if (dontFailWhenClosed) return;
if (failSilently) return;
throw new Error('not opened');
}

options = options || {};
if (options.mask === undefined) options.mask = !this._isServer;

this._sender.ping(data, options);
if (mask === undefined) mask = !this._isServer;
this._sender.ping(data, mask);
}

/**
* Send a pong message.
*
* @param {*} data The message to send
* @param {Object} options Options object
* @param {Boolean} options.mask Indicates whether or not to mask `data`
* @param {Boolean} dontFailWhenClosed Indicates whether or not to throw an if the connection isn't open
* @param {Boolean} mask Indicates whether or not to mask `data`
* @param {Boolean} failSilently Indicates whether or not to throw if `readyState` isn't `OPEN`
* @public
*/
pong (data, options, dontFailWhenClosed) {
pong (data, mask, failSilently) {
if (this.readyState !== WebSocket.OPEN) {
if (dontFailWhenClosed) return;
if (failSilently) return;
throw new Error('not opened');
}

options = options || {};
if (options.mask === undefined) options.mask = !this._isServer;

this._sender.pong(data, options);
if (mask === undefined) mask = !this._isServer;
this._sender.pong(data, mask);
}

/**
Expand Down
9 changes: 4 additions & 5 deletions test/Sender.test.js
Expand Up @@ -70,12 +70,11 @@ describe('Sender', function () {
});

const array = new Uint8Array([0x68, 0x69]);
const options = { mask: false };

sender.ping(array.buffer, options);
sender.ping(array, options);
sender.ping('hi', options);
sender.ping(10, options);
sender.ping(array.buffer, false);
sender.ping(array, false);
sender.ping('hi', false);
sender.ping(10, false);
});
});

Expand Down
8 changes: 4 additions & 4 deletions test/WebSocket.test.js
Expand Up @@ -483,7 +483,7 @@ describe('WebSocket', function () {
const ws = new WebSocket(`ws://localhost:${port}`);

ws.on('error', () => {});
ws.ping('', {}, true);
ws.ping('', true, true);

srv.close(done);
ws.terminate();
Expand Down Expand Up @@ -542,7 +542,7 @@ describe('WebSocket', function () {

const ws = new WebSocket(`ws://localhost:${port}`);

ws.on('open', () => ws.ping('hi', { mask: true }));
ws.on('open', () => ws.ping('hi', true));
});
});
});
Expand All @@ -568,7 +568,7 @@ describe('WebSocket', function () {
const ws = new WebSocket(`ws://localhost:${port}`);

ws.on('error', () => {});
ws.pong('', {}, true);
ws.pong('', true, true);

srv.close(done);
ws.terminate();
Expand Down Expand Up @@ -613,7 +613,7 @@ describe('WebSocket', function () {

const ws = new WebSocket(`ws://localhost:${port}`);

ws.on('open', () => ws.pong('hi', { mask: true }));
ws.on('open', () => ws.pong('hi', true));
});
});
});
Expand Down

0 comments on commit b187775

Please sign in to comment.