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.

126 lines
2.3 KiB

4 years ago
  1. /**
  2. * Module dependencies.
  3. */
  4. var http = require('http');
  5. /**
  6. * Invoking the library as a function delegates to attach if the first argument
  7. * is an `http.Server`.
  8. *
  9. * If there are no arguments or the first argument is an options object, then
  10. * a new Server instance is returned.
  11. *
  12. * @param {http.Server} server (if specified, will be attached to by the new Server instance)
  13. * @param {Object} options
  14. * @return {Server} engine server
  15. * @api public
  16. */
  17. exports = module.exports = function () {
  18. // backwards compatible use as `.attach`
  19. // if first argument is an http server
  20. if (arguments.length && arguments[0] instanceof http.Server) {
  21. return attach.apply(this, arguments);
  22. }
  23. // if first argument is not an http server, then just make a regular eio server
  24. return exports.Server.apply(null, arguments);
  25. };
  26. /**
  27. * Protocol revision number.
  28. *
  29. * @api public
  30. */
  31. exports.protocol = 1;
  32. /**
  33. * Expose Server constructor.
  34. *
  35. * @api public
  36. */
  37. exports.Server = require('./server');
  38. /**
  39. * Expose Socket constructor.
  40. *
  41. * @api public
  42. */
  43. exports.Socket = require('./socket');
  44. /**
  45. * Expose Transport constructor.
  46. *
  47. * @api public
  48. */
  49. exports.Transport = require('./transport');
  50. /**
  51. * Expose mutable list of available transports.
  52. *
  53. * @api public
  54. */
  55. exports.transports = require('./transports');
  56. /**
  57. * Exports parser.
  58. *
  59. * @api public
  60. */
  61. exports.parser = require('engine.io-parser');
  62. /**
  63. * Creates an http.Server exclusively used for WS upgrades.
  64. *
  65. * @param {Number} port
  66. * @param {Function} callback
  67. * @param {Object} options
  68. * @return {Server} websocket.io server
  69. * @api public
  70. */
  71. exports.listen = listen;
  72. function listen (port, options, fn) {
  73. if ('function' === typeof options) {
  74. fn = options;
  75. options = {};
  76. }
  77. var server = http.createServer(function (req, res) {
  78. res.writeHead(501);
  79. res.end('Not Implemented');
  80. });
  81. // create engine server
  82. var engine = exports.attach(server, options);
  83. engine.httpServer = server;
  84. server.listen(port, fn);
  85. return engine;
  86. }
  87. /**
  88. * Captures upgrade requests for a http.Server.
  89. *
  90. * @param {http.Server} server
  91. * @param {Object} options
  92. * @return {Server} engine server
  93. * @api public
  94. */
  95. exports.attach = attach;
  96. function attach (server, options) {
  97. var engine = new exports.Server(options);
  98. engine.attach(server, options);
  99. return engine;
  100. }