From 9481cc6cf9167f08a50b8365db63e6988b651ade Mon Sep 17 00:00:00 2001 From: petea Date: Fri, 19 Dec 2014 07:10:34 +0200 Subject: [PATCH] #52 --- lib/index.js | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/lib/index.js b/lib/index.js index 4f5194f..73b2d06 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,13 +1,43 @@ +/* + * The skeleton of this file is gotten from socket.io source code. + * + * If you are looking for a original code then you are on wrong way. + * All this staff is intended to be for study porpuse. + */ var Manager = require('./manager'); var Socket = require('./socket'); var Emitter = require('./eventemitter'); -module.exports = io; +var util = require('./util'); +var url = require('url'); -function io(url, opts) { +module.exports = lookup; + +var cache = exports.managers = {}; + +function lookup(url, opts) { + if(! util.isString(url)) throw new Error('url is expected to be string'); + + //I do not check if opts is object or not. The programmer is in charge with this moment opts = opts || {}; + + var urlInfo = url.parse(url); + var id = urlInfo.protocol + '//' + urlInfo.host; //protocol + hostname + port + var io; + + if(opts.forceNew || opts['force new connection'] || opts.multiplex === false){ + io = new Manager(url, opts); + } else { + if( ! cache[id]) { + cache[id] = new Manager(url, opts); + } + + io = cache[id]; + } + + return io.socket(urlInfo.pathname); } -io.Manager = Manager; -io.Socket = Socket; -io.Emitter = Emitter; +lookup.Manager = Manager; +lookup.Socket = Socket; +lookup.Emitter = Emitter;