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.

connection.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* global APP, JitsiMeetJS, config */
  2. import AuthHandler from './modules/UI/authentication/AuthHandler';
  3. import jitsiLocalStorage from './modules/util/JitsiLocalStorage';
  4. import {
  5. connectionEstablished,
  6. connectionFailed
  7. } from './react/features/base/connection';
  8. import {
  9. isFatalJitsiConnectionError
  10. } from './react/features/base/lib-jitsi-meet';
  11. const ConnectionEvents = JitsiMeetJS.events.connection;
  12. const ConnectionErrors = JitsiMeetJS.errors.connection;
  13. const logger = require("jitsi-meet-logger").getLogger(__filename);
  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. const connectionConfig = Object.assign({}, config);
  59. const { issuer, jwt } = APP.store.getState()['features/jwt'];
  60. connectionConfig.bosh += '?room=' + roomName;
  61. let connection
  62. = new JitsiMeetJS.JitsiConnection(
  63. null,
  64. jwt && issuer && issuer !== 'anonymous' ? jwt : undefined,
  65. connectionConfig);
  66. return new Promise(function (resolve, reject) {
  67. connection.addEventListener(
  68. ConnectionEvents.CONNECTION_ESTABLISHED,
  69. handleConnectionEstablished);
  70. connection.addEventListener(
  71. ConnectionEvents.CONNECTION_FAILED,
  72. handleConnectionFailed);
  73. connection.addEventListener(
  74. ConnectionEvents.CONNECTION_FAILED,
  75. connectionFailedHandler);
  76. function connectionFailedHandler(error, message, credentials) {
  77. APP.store.dispatch(
  78. connectionFailed(connection, error, message, credentials));
  79. if (isFatalJitsiConnectionError(error)) {
  80. connection.removeEventListener(
  81. ConnectionEvents.CONNECTION_FAILED,
  82. connectionFailedHandler);
  83. }
  84. }
  85. function unsubscribe() {
  86. connection.removeEventListener(
  87. ConnectionEvents.CONNECTION_ESTABLISHED,
  88. handleConnectionEstablished);
  89. connection.removeEventListener(
  90. ConnectionEvents.CONNECTION_FAILED,
  91. handleConnectionFailed);
  92. }
  93. function handleConnectionEstablished() {
  94. APP.store.dispatch(connectionEstablished(connection));
  95. unsubscribe();
  96. resolve(connection);
  97. }
  98. function handleConnectionFailed(err) {
  99. unsubscribe();
  100. logger.error("CONNECTION FAILED:", err);
  101. reject(err);
  102. }
  103. checkForAttachParametersAndConnect(id, password, connection);
  104. });
  105. }
  106. /**
  107. * Open JitsiConnection using provided credentials.
  108. * If retry option is true it will show auth dialog on PASSWORD_REQUIRED error.
  109. *
  110. * @param {object} options
  111. * @param {string} [options.id]
  112. * @param {string} [options.password]
  113. * @param {string} [options.roomName]
  114. * @param {boolean} [retry] if we should show auth dialog
  115. * on PASSWORD_REQUIRED error.
  116. *
  117. * @returns {Promise<JitsiConnection>}
  118. */
  119. export function openConnection({id, password, retry, roomName}) {
  120. let usernameOverride
  121. = jitsiLocalStorage.getItem("xmpp_username_override");
  122. let passwordOverride
  123. = jitsiLocalStorage.getItem("xmpp_password_override");
  124. if (usernameOverride && usernameOverride.length > 0) {
  125. id = usernameOverride;
  126. }
  127. if (passwordOverride && passwordOverride.length > 0) {
  128. password = passwordOverride;
  129. }
  130. return connect(id, password, roomName).catch(err => {
  131. if (retry) {
  132. const { issuer, jwt } = APP.store.getState()['features/jwt'];
  133. if (err === ConnectionErrors.PASSWORD_REQUIRED
  134. && (!jwt || issuer === 'anonymous')) {
  135. return AuthHandler.requestAuth(roomName, connect);
  136. }
  137. }
  138. throw err;
  139. });
  140. }