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.

external_connect.js 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 successCallback 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( // eslint-disable-line no-unused-vars
  24. webserviceUrl,
  25. successCallback,
  26. error_callback) {
  27. if (!window.XMLHttpRequest) {
  28. error_callback(new Error('XMLHttpRequest is not supported!'));
  29. return;
  30. }
  31. var HTTP_STATUS_OK = 200;
  32. var xhttp = new XMLHttpRequest();
  33. xhttp.onreadystatechange = function() {
  34. if (xhttp.readyState == xhttp.DONE) {
  35. var now = window.connectionTimes['external_connect.done']
  36. = window.performance.now();
  37. console.log('(TIME) external connect XHR done:\t', now);
  38. if (xhttp.status == HTTP_STATUS_OK) {
  39. try {
  40. var data = JSON.parse(xhttp.responseText);
  41. var proxyRegion = xhttp.getResponseHeader('X-Proxy-Region');
  42. var jitsiRegion = xhttp.getResponseHeader('X-Jitsi-Region');
  43. window.jitsiRegionInfo = {
  44. 'ProxyRegion' : proxyRegion,
  45. 'Region' : jitsiRegion,
  46. 'Shard' : xhttp.getResponseHeader('X-Jitsi-Shard'),
  47. 'CrossRegion': proxyRegion !== jitsiRegion ? 1 : 0
  48. };
  49. successCallback(data);
  50. } catch (e) {
  51. error_callback(e);
  52. }
  53. } else {
  54. error_callback(new Error('XMLHttpRequest error. Status: '
  55. + xhttp.status + '. Error message: ' + xhttp.statusText));
  56. }
  57. }
  58. };
  59. xhttp.open('GET', webserviceUrl, true);
  60. // Fixes external connect for IE
  61. // The timeout property may be set only after calling the open() method
  62. // and before calling the send() method.
  63. xhttp.timeout = 3000;
  64. window.connectionTimes = {};
  65. var now = window.connectionTimes['external_connect.sending']
  66. = window.performance.now();
  67. console.log('(TIME) Sending external connect XHR:\t', now);
  68. xhttp.send();
  69. }