Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

external_connect.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Requests the given webservice that will create the connection and will return
  3. * the necessary details(rid, sid and jid) to attach to this connection and
  4. * start using it. This script can be used for optimizing the connection startup
  5. * time. The function will send AJAX request to a webservice that should
  6. * create the bosh session much faster than the client because the webservice
  7. * can be started on the same machine as the XMPP serever.
  8. *
  9. * NOTE: It's vert important to execute this function as early as you can for
  10. * optimal results.
  11. *
  12. * @param webserviceUrl the url for the web service that is going to create the
  13. * connection.
  14. * @param success_callback callback function called with the result of the AJAX
  15. * request if the request was successfull. The callback will receive one
  16. * parameter which will be JS Object with properties - rid, sid and jid. This
  17. * result should be passed to JitsiConnection.attach method in order to use that
  18. * connection.
  19. * @param error_callback callback function called the AJAX request fail. This
  20. * callback is going to receive one parameter which is going to be JS error
  21. * object with a reason for failure in it.
  22. */
  23. function createConnectionExternally(webserviceUrl, success_callback,
  24. error_callback) {
  25. if (!window.XMLHttpRequest) {
  26. error_callback(new Error("XMLHttpRequest is not supported!"));
  27. return;
  28. }
  29. var HTTP_STATUS_OK = 200;
  30. var xhttp = new XMLHttpRequest();
  31. xhttp.onreadystatechange = function() {
  32. if (xhttp.readyState == xhttp.DONE) {
  33. var now = window.connectionTimes["external_connect.done"] =
  34. window.performance.now();
  35. console.log("(TIME) external connect XHR done:\t", now);
  36. if (xhttp.status == HTTP_STATUS_OK) {
  37. try {
  38. var data = JSON.parse(xhttp.responseText);
  39. window.jitsiRegionInfo = {
  40. "ProxyRegion" :
  41. xhttp.getResponseHeader('X-Proxy-Region'),
  42. "Region" :
  43. xhttp.getResponseHeader('X-Jitsi-Region'),
  44. "Shard" :
  45. xhttp.getResponseHeader('X-Jitsi-Shard')
  46. };
  47. success_callback(data);
  48. } catch (e) {
  49. error_callback(e);
  50. }
  51. } else {
  52. error_callback(new Error("XMLHttpRequest error. Status: " +
  53. xhttp.status + ". Error message: " + xhttp.statusText));
  54. }
  55. }
  56. };
  57. xhttp.open("GET", webserviceUrl, true);
  58. // Fixes external connect for IE
  59. // The timeout property may be set only after calling the open() method
  60. // and before calling the send() method.
  61. xhttp.timeout = 3000;
  62. window.connectionTimes = {};
  63. var now = window.connectionTimes["external_connect.sending"] =
  64. window.performance.now();
  65. console.log("(TIME) Sending external connect XHR:\t", now);
  66. xhttp.send();
  67. }