You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.0 KiB

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