| 1 | 'use strict'; |
| 2 | |
| 3 | /*! |
| 4 | * ws: a node.js websocket client |
| 5 | * Copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com> |
| 6 | * MIT Licensed |
| 7 | */ |
| 8 | |
| 9 | var WS = module.exports = require('./lib/WebSocket'); |
| 10 | |
| 11 | WS.Server = require('./lib/WebSocketServer'); |
| 12 | WS.Sender = require('./lib/Sender'); |
| 13 | WS.Receiver = require('./lib/Receiver'); |
| 14 | |
| 15 | /** |
| 16 | * Create a new WebSocket server. |
| 17 | * |
| 18 | * @param {Object} options Server options |
| 19 | * @param {Function} fn Optional connection listener. |
| 20 | * @returns {WS.Server} |
| 21 | * @api public |
| 22 | */ |
| 23 | WS.createServer = function createServer(options, fn) { |
| 24 | var server = new WS.Server(options); |
| 25 | |
| 26 | if (typeof fn === 'function') { |
| 27 | server.on('connection', fn); |
| 28 | } |
| 29 | |
| 30 | return server; |
| 31 | }; |
| 32 | |
| 33 | /** |
| 34 | * Create a new WebSocket connection. |
| 35 | * |
| 36 | * @param {String} address The URL/address we need to connect to. |
| 37 | * @param {Function} fn Open listener. |
| 38 | * @returns {WS} |
| 39 | * @api public |
| 40 | */ |
| 41 | WS.connect = WS.createConnection = function connect(address, fn) { |
| 42 | var client = new WS(address); |
| 43 | |
| 44 | if (typeof fn === 'function') { |
| 45 | client.on('open', fn); |
| 46 | } |
| 47 | |
| 48 | return client; |
| 49 | }; |