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.

53 lines
1.1 KiB

7 years ago
  1. /**
  2. * Module dependencies
  3. */
  4. var XMLHttpRequest = require('xmlhttprequest-ssl');
  5. var XHR = require('./polling-xhr');
  6. var JSONP = require('./polling-jsonp');
  7. var websocket = require('./websocket');
  8. /**
  9. * Export transports.
  10. */
  11. exports.polling = polling;
  12. exports.websocket = websocket;
  13. /**
  14. * Polling transport polymorphic constructor.
  15. * Decides on xhr vs jsonp based on feature detection.
  16. *
  17. * @api private
  18. */
  19. function polling (opts) {
  20. var xhr;
  21. var xd = false;
  22. var xs = false;
  23. var jsonp = false !== opts.jsonp;
  24. if (global.location) {
  25. var isSSL = 'https:' === location.protocol;
  26. var port = location.port;
  27. // some user agents have empty `location.port`
  28. if (!port) {
  29. port = isSSL ? 443 : 80;
  30. }
  31. xd = opts.hostname !== location.hostname || port !== opts.port;
  32. xs = opts.secure !== isSSL;
  33. }
  34. opts.xdomain = xd;
  35. opts.xscheme = xs;
  36. xhr = new XMLHttpRequest(opts);
  37. if ('open' in xhr && !opts.forceJSONP) {
  38. return new XHR(opts);
  39. } else {
  40. if (!jsonp) throw new Error('JSONP disabled');
  41. return new JSONP(opts);
  42. }
  43. }