Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

connection.js 5.2KB

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