-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[minor] Clean up
WebSocket.createServer()
and WebSocket.connect()
- Loading branch information
Showing
3 changed files
with
110 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters