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.

87 lines
1.5 KiB

  1. /**
  2. * Module dependencies.
  3. */
  4. var url = require('./url');
  5. var parser = require('socket.io-parser');
  6. var Manager = require('./manager');
  7. var debug = require('debug')('socket.io-client');
  8. /**
  9. * Module exports.
  10. */
  11. module.exports = exports = lookup;
  12. /**
  13. * Managers cache.
  14. */
  15. var cache = exports.managers = {};
  16. /**
  17. * Looks up an existing `Manager` for multiplexing.
  18. * If the user summons:
  19. *
  20. * `io('http://localhost/a');`
  21. * `io('http://localhost/b');`
  22. *
  23. * We reuse the existing instance based on same scheme/port/host,
  24. * and we initialize sockets for each namespace.
  25. *
  26. * @api public
  27. */
  28. function lookup(uri, opts) {
  29. if (typeof uri == 'object') {
  30. opts = uri;
  31. uri = undefined;
  32. }
  33. opts = opts || {};
  34. var parsed = url(uri);
  35. var source = parsed.source;
  36. var id = parsed.id;
  37. var io;
  38. if (opts.forceNew || opts['force new connection'] || false === opts.multiplex) {
  39. debug('ignoring socket cache for %s', source);
  40. io = Manager(source, opts);
  41. } else {
  42. if (!cache[id]) {
  43. debug('new io instance for %s', source);
  44. cache[id] = Manager(source, opts);
  45. }
  46. io = cache[id];
  47. }
  48. return io.socket(parsed.path);
  49. }
  50. /**
  51. * Protocol version.
  52. *
  53. * @api public
  54. */
  55. exports.protocol = parser.protocol;
  56. /**
  57. * `connect`.
  58. *
  59. * @param {String} uri
  60. * @api public
  61. */
  62. exports.connect = lookup;
  63. /**
  64. * Expose constructors for standalone build.
  65. *
  66. * @api public
  67. */
  68. exports.Manager = require('./manager');
  69. exports.Socket = require('./socket');