123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- var url = require('./url');
- var parser = require('socket.io-parser');
- var Manager = require('./manager');
- var debug = require('debug')('socket.io-client');
- module.exports = exports = lookup;
- var cache = exports.managers = {};
- function lookup (uri, opts) {
- if (typeof uri === 'object') {
- opts = uri;
- uri = undefined;
- }
- opts = opts || {};
- var parsed = url(uri);
- var source = parsed.source;
- var id = parsed.id;
- var path = parsed.path;
- var sameNamespace = cache[id] && path in cache[id].nsps;
- var newConnection = opts.forceNew || opts['force new connection'] ||
- false === opts.multiplex || sameNamespace;
- var io;
- if (newConnection) {
- debug('ignoring socket cache for %s', source);
- io = Manager(source, opts);
- } else {
- if (!cache[id]) {
- debug('new io instance for %s', source);
- cache[id] = Manager(source, opts);
- }
- io = cache[id];
- }
- if (parsed.query && !opts.query) {
- opts.query = parsed.query;
- } else if (opts && 'object' === typeof opts.query) {
- opts.query = encodeQueryString(opts.query);
- }
- return io.socket(parsed.path, opts);
- }
- function encodeQueryString (obj) {
- var str = [];
- for (var p in obj) {
- if (obj.hasOwnProperty(p)) {
- str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]));
- }
- }
- return str.join('&');
- }
- exports.protocol = parser.protocol;
- exports.connect = lookup;
- exports.Manager = require('./manager');
- exports.Socket = require('./socket');
|