Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

connection.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* global APP, JitsiMeetJS, config */
  2. import AuthHandler from './modules/UI/authentication/AuthHandler';
  3. import jitsiLocalStorage from './modules/util/JitsiLocalStorage';
  4. const ConnectionEvents = JitsiMeetJS.events.connection;
  5. const ConnectionErrors = JitsiMeetJS.errors.connection;
  6. /**
  7. * Checks if we have data to use attach instead of connect. If we have the data
  8. * executes attach otherwise check if we have to wait for the data. If we have
  9. * to wait for the attach data we are setting handler to APP.connect.handler
  10. * which is going to be called when the attach data is received otherwise
  11. * executes connect.
  12. *
  13. * @param {string} [id] user id
  14. * @param {string} [password] password
  15. * @param {string} [roomName] the name of the conference.
  16. */
  17. function checkForAttachParametersAndConnect(id, password, connection) {
  18. if(window.XMPPAttachInfo){
  19. APP.connect.status = "connecting";
  20. // When connection optimization is not deployed or enabled the default
  21. // value will be window.XMPPAttachInfo.status = "error"
  22. // If the connection optimization is deployed and enabled and there is
  23. // a failure the value will be window.XMPPAttachInfo.status = "error"
  24. if(window.XMPPAttachInfo.status === "error") {
  25. connection.connect({id, password});
  26. return;
  27. }
  28. var attachOptions = window.XMPPAttachInfo.data;
  29. if(attachOptions) {
  30. connection.attach(attachOptions);
  31. delete window.XMPPAttachInfo.data;
  32. } else {
  33. connection.connect({id, password});
  34. }
  35. } else {
  36. APP.connect.status = "ready";
  37. APP.connect.handler = checkForAttachParametersAndConnect.bind(null,
  38. id, password, connection);
  39. }
  40. }
  41. /**
  42. * Try to open connection using provided credentials.
  43. * @param {string} [id]
  44. * @param {string} [password]
  45. * @param {string} [roomName]
  46. * @returns {Promise<JitsiConnection>} connection if
  47. * everything is ok, else error.
  48. */
  49. function connect(id, password, roomName) {
  50. let connectionConfig = Object.assign({}, config);
  51. connectionConfig.bosh += '?room=' + roomName;
  52. let connection
  53. = new JitsiMeetJS.JitsiConnection(null, config.token, connectionConfig);
  54. return new Promise(function (resolve, reject) {
  55. connection.addEventListener(
  56. ConnectionEvents.CONNECTION_ESTABLISHED, handleConnectionEstablished
  57. );
  58. connection.addEventListener(
  59. ConnectionEvents.CONNECTION_FAILED, handleConnectionFailed
  60. );
  61. function unsubscribe() {
  62. connection.removeEventListener(
  63. ConnectionEvents.CONNECTION_ESTABLISHED,
  64. handleConnectionEstablished
  65. );
  66. connection.removeEventListener(
  67. ConnectionEvents.CONNECTION_FAILED,
  68. handleConnectionFailed
  69. );
  70. }
  71. function handleConnectionEstablished() {
  72. unsubscribe();
  73. resolve(connection);
  74. }
  75. function handleConnectionFailed(err) {
  76. unsubscribe();
  77. console.error("CONNECTION FAILED:", err);
  78. reject(err);
  79. }
  80. checkForAttachParametersAndConnect(id, password, connection);
  81. });
  82. }
  83. /**
  84. * Open JitsiConnection using provided credentials.
  85. * If retry option is true it will show auth dialog on PASSWORD_REQUIRED error.
  86. *
  87. * @param {object} options
  88. * @param {string} [options.id]
  89. * @param {string} [options.password]
  90. * @param {string} [options.roomName]
  91. * @param {boolean} [retry] if we should show auth dialog
  92. * on PASSWORD_REQUIRED error.
  93. *
  94. * @returns {Promise<JitsiConnection>}
  95. */
  96. export function openConnection({id, password, retry, roomName}) {
  97. let usernameOverride
  98. = jitsiLocalStorage.getItem("xmpp_username_override");
  99. let passwordOverride
  100. = jitsiLocalStorage.getItem("xmpp_password_override");
  101. if (usernameOverride && usernameOverride.length > 0) {
  102. id = usernameOverride;
  103. }
  104. if (passwordOverride && passwordOverride.length > 0) {
  105. password = passwordOverride;
  106. }
  107. return connect(id, password, roomName).catch(function (err) {
  108. if (!retry) {
  109. throw err;
  110. }
  111. if (err === ConnectionErrors.PASSWORD_REQUIRED) {
  112. // do not retry if token is not valid
  113. if (config.token) {
  114. throw err;
  115. } else {
  116. return AuthHandler.requestAuth(roomName, connect);
  117. }
  118. } else {
  119. throw err;
  120. }
  121. });
  122. }