Skip to content

Commit

Permalink
#52
Browse files Browse the repository at this point in the history
  • Loading branch information
smart--petea committed Dec 19, 2014
1 parent c19890c commit 9481cc6
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 9481cc6

Please sign in to comment.