Skip to content

Commit

Permalink
allow #ping and #pong to fail silently for already closed connections.
Browse files Browse the repository at this point in the history
…fixes #30
  • Loading branch information
einaros committed Feb 28, 2012
1 parent cd6e455 commit 8c3f6d7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/WebSocket.js
Expand Up @@ -130,11 +130,15 @@ WebSocket.prototype.pause = function() {
*
* @param {Object} data to be sent to the server
* @param {Object} Members - mask: boolean, binary: boolean
* @param {boolean} dontFailWhenClosed indicates whether or not to throw if the connection isnt open
* @api public
*/

WebSocket.prototype.ping = function(data, options) {
if (this.readyState != WebSocket.OPEN) throw new Error('not opened');
WebSocket.prototype.ping = function(data, options, dontFailWhenClosed) {
if (this.readyState != WebSocket.OPEN) {
if (dontFailWhenClosed === true) return;
throw new Error('not opened');
}
options = options || {};
if (typeof options.mask == 'undefined') options.mask = !this._isServer;
this._sender.ping(data, options);
Expand All @@ -145,11 +149,15 @@ WebSocket.prototype.ping = function(data, options) {
*
* @param {Object} data to be sent to the server
* @param {Object} Members - mask: boolean, binary: boolean
* @param {boolean} dontFailWhenClosed indicates whether or not to throw if the connection isnt open
* @api public
*/

WebSocket.prototype.pong = function(data, options) {
if (this.readyState != WebSocket.OPEN) throw new Error('not opened');
WebSocket.prototype.pong = function(data, options, dontFailWhenClosed) {
if (this.readyState != WebSocket.OPEN) {
if (dontFailWhenClosed === true) return;
throw new Error('not opened');
}
options = options || {};
if (typeof options.mask == 'undefined') options.mask = !this._isServer;
this._sender.pong(data, options);
Expand Down Expand Up @@ -535,7 +543,7 @@ function establishConnection(ReceiverClass, SenderClass, socket, upgradeHead) {
});
receiver.on('ping', function(data, flags) {
flags = flags || {};
self.pong(data, {mask: !self._isServer, binary: flags.binary === true});
self.pong(data, {mask: !self._isServer, binary: flags.binary === true}, true);
self.emit('ping', data, flags);
});
receiver.on('pong', function(data, flags) {
Expand Down
37 changes: 37 additions & 0 deletions test/WebSocket.test.js
Expand Up @@ -287,6 +287,17 @@ describe('WebSocket', function() {
});
});

it('before connect can silently fail', function(done) {
server.createServer(++port, function(srv) {
var ws = new WebSocket('ws://localhost:' + port);
ws.on('error', function() {});
ws.ping('', {}, true);
srv.close();
ws.terminate();
done();
});
});

it('without message is successfully transmitted to the server', function(done) {
server.createServer(++port, function(srv) {
var ws = new WebSocket('ws://localhost:' + port);
Expand Down Expand Up @@ -334,6 +345,32 @@ describe('WebSocket', function() {
});

describe('#pong', function() {
it('before connect should fail', function(done) {
server.createServer(++port, function(srv) {
var ws = new WebSocket('ws://localhost:' + port);
ws.on('error', function() {});
try {
ws.pong();
}
catch (e) {
srv.close();
ws.terminate();
done();
}
});
});

it('before connect can silently fail', function(done) {
server.createServer(++port, function(srv) {
var ws = new WebSocket('ws://localhost:' + port);
ws.on('error', function() {});
ws.pong('', {}, true);
srv.close();
ws.terminate();
done();
});
});

it('without message is successfully transmitted to the server', function(done) {
server.createServer(++port, function(srv) {
var ws = new WebSocket('ws://localhost:' + port);
Expand Down

0 comments on commit 8c3f6d7

Please sign in to comment.