You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.0 KiB
45 lines
1.0 KiB
const XMLHttpRequest = require("xmlhttprequest-ssl");
|
|
const XHR = require("./polling-xhr");
|
|
const JSONP = require("./polling-jsonp");
|
|
const websocket = require("./websocket");
|
|
|
|
exports.polling = polling;
|
|
exports.websocket = websocket;
|
|
|
|
/**
|
|
* Polling transport polymorphic constructor.
|
|
* Decides on xhr vs jsonp based on feature detection.
|
|
*
|
|
* @api private
|
|
*/
|
|
|
|
function polling(opts) {
|
|
let xhr;
|
|
let xd = false;
|
|
let xs = false;
|
|
const jsonp = false !== opts.jsonp;
|
|
|
|
if (typeof location !== "undefined") {
|
|
const isSSL = "https:" === location.protocol;
|
|
let port = location.port;
|
|
|
|
// some user agents have empty `location.port`
|
|
if (!port) {
|
|
port = isSSL ? 443 : 80;
|
|
}
|
|
|
|
xd = opts.hostname !== location.hostname || port !== opts.port;
|
|
xs = opts.secure !== isSSL;
|
|
}
|
|
|
|
opts.xdomain = xd;
|
|
opts.xscheme = xs;
|
|
xhr = new XMLHttpRequest(opts);
|
|
|
|
if ("open" in xhr && !opts.forceJSONP) {
|
|
return new XHR(opts);
|
|
} else {
|
|
if (!jsonp) throw new Error("JSONP disabled");
|
|
return new JSONP(opts);
|
|
}
|
|
}
|
|
|