mirror of
https://github.com/socketio/socket.io.git
synced 2026-04-30 03:00:39 -04:00
socket: constructor can accept uri/opts simultaneously
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user