Skip to content

Commit

Permalink
[minor] Clean up WebSocket.createServer() and WebSocket.connect()
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Dec 6, 2016
1 parent 22d3b77 commit 7ef052a
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 23 deletions.
57 changes: 34 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,60 @@
'use strict';

/*!
* ws: a node.js websocket client
* Copyright(c) 2011 Einar Otto Stangvik <[email protected]>
* MIT Licensed
*/

var WS = module.exports = require('./lib/WebSocket');
'use strict';

const WS = module.exports = require('./lib/WebSocket');

WS.Server = require('./lib/WebSocketServer');
WS.Sender = require('./lib/Sender');
WS.Receiver = require('./lib/Receiver');
WS.Sender = require('./lib/Sender');

/**
* Create a new WebSocket server.
* A factory function, which returns a new `WebSocketServer`.
*
* @param {Object} options Server options
* @param {Function} fn Optional connection listener.
* @returns {WS.Server}
* @api public
* @param {Object} options Configuration options
* @param {Function} fn A listener for the `connection` event
* @return {WebSocketServer}
* @public
*/
WS.createServer = function createServer (options, fn) {
var server = new WS.Server(options);

if (typeof fn === 'function') {
server.on('connection', fn);
}
const server = new WS.Server(options);

if (fn) server.on('connection', fn);
return server;
};

/**
* Create a new WebSocket connection.
* A factory function, which returns a new `WebSocket` and automatically
* connectes to the supplied address.
*
* @param {String} address The URL/address we need to connect to.
* @param {Function} fn Open listener.
* @returns {WS}
* @api public
* @param {String} address The URL to which to connect
* @param {(String|String[])} protocols The list of subprotocols
* @param {Object} options Connection options
* @param {Function} fn A listener for the `open` event
* @return {WebSocket}
* @public
*/
WS.connect = WS.createConnection = function connect (address, fn) {
var client = new WS(address);
WS.connect = WS.createConnection = function connect (address, protocols, options, fn) {
if (typeof protocols === 'function') {
fn = protocols;
protocols = options = null;
} else if (typeof protocols === 'object' && !Array.isArray(protocols)) {
fn = options;
options = protocols;
protocols = null;
}

if (typeof fn === 'function') {
client.on('open', fn);
if (typeof options === 'function') {
fn = options;
options = null;
}

const client = new WS(address, protocols, options);

if (fn) client.on('open', fn);
return client;
};
61 changes: 61 additions & 0 deletions test/WebSocket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2250,3 +2250,64 @@ describe('WebSocket', function () {
});
});
});

describe('WebSocket.connect', function () {
it('creates a `WebSocket` instance', function (done) {
const wss = new WebSocketServer({ port: ++port }, () => {
const ws = WebSocket.connect(`ws://localhost:${port}`);

ws.on('open', () => wss.close(done));
});
});

it('can add a listener for the `open` event', function (done) {
const wss = new WebSocketServer({ port: ++port }, () => {
const ws = WebSocket.connect(`ws://localhost:${port}`, () => {
wss.close(done);
});
});
});

it('allows to specify a subprotocol', function (done) {
const wss = new WebSocketServer({ port: ++port }, () => {
const ws = WebSocket.connect(`ws://localhost:${port}`, 'foo', () => {
assert.strictEqual(ws.protocol, 'foo');
wss.close(done);
});
});
});

it('allows to specify a list of subprotocols', function (done) {
const wss = new WebSocketServer({ port: ++port }, () => {
const ws = WebSocket.connect(`ws://localhost:${port}`, ['foo', 'bar'], () => {
assert.strictEqual(ws.protocol, 'foo');
wss.close(done);
});
});
});

it('allows to use connection options', function (done) {
const wss = new WebSocketServer({ port: ++port }, () => {
const ws = WebSocket.connect(`ws://localhost:${port}`, {
protocolVersion: 8
}, () => {
assert.strictEqual(ws.protocolVersion, 8);
wss.close(done);
});
});
});

it('allows to specify a subprotocol and use connection options', function (done) {
const wss = new WebSocketServer({ port: ++port }, () => {
const ws = WebSocket.connect(`ws://localhost:${port}`, 'foo', {
protocolVersion: 8
});

ws.on('open', () => {
assert.strictEqual(ws.protocolVersion, 8);
assert.strictEqual(ws.protocol, 'foo');
wss.close(done);
});
});
});
});
15 changes: 15 additions & 0 deletions test/WebSocketServer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1046,3 +1046,18 @@ describe('WebSocketServer', function () {
});
});
});

describe('WebSocket.createServer', function () {
it('creates a `WebSocketServer` instance', function () {
const wss = WebSocket.createServer({ noServer: true });

assert.ok(wss instanceof WebSocketServer);
});

it('can add a listener for the `connection` event', function () {
const connectionListener = () => {};
const wss = WebSocket.createServer({ noServer: true }, connectionListener);

assert.strictEqual(wss.listeners('connection')[0], connectionListener);
});
});

0 comments on commit 7ef052a

Please sign in to comment.