socket: constructor can accept uri/opts simultaneously

This commit is contained in:
Guillermo Rauch
2012-12-16 20:34:18 -03:00
parent 0660b5ce27
commit 00a5640a12

View File

@@ -22,30 +22,43 @@ var global = util.global();
/**
* Socket constructor.
*
* @param {String|Object} uri or options
* @param {Object} options
* @api public
*/
function Socket(opts){
if (!(this instanceof Socket)) return new Socket(opts);
function Socket(uri, opts){
if (!(this instanceof Socket)) return new Socket(uri, opts);
if ('string' == typeof opts) {
var uri = util.parseUri(opts);
opts = opts || {};
if ('object' == typeof uri) {
opts = uri;
uri = null;
}
if (uri) {
uri = util.parseUri(uri);
opts = arguments[1] || {};
opts.host = uri.host;
opts.secure = uri.protocol == 'https' || uri.protocol == 'wss';
opts.port = uri.port;
}
opts = opts || {};
this.secure = null != opts.secure ? opts.secure : (global.location && 'https:' == location.protocol);
this.secure = null != opts.secure ? opts.secure :
(global.location && 'https:' == location.protocol);
if (opts.host) {
var pieces = opts.host.split(':');
opts.hostname = pieces.shift();
if (pieces.length) opts.port = pieces.pop();
}
this.hostname = opts.hostname || (global.location ? location.hostname : 'localhost');
this.port = opts.port || (global.location && location.port ? location.port : (this.secure ? 443 : 80));
this.hostname = opts.hostname ||
(global.location ? location.hostname : 'localhost');
this.port = opts.port || (global.location && location.port ?
location.port :
(this.secure ? 443 : 80));
this.query = opts.query || {};
this.query.uid = rnd();
this.upgrade = false !== opts.upgrade;