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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. JitsiConnectionErrors,
  11. JitsiConnectionEvents
  12. } from './react/features/base/lib-jitsi-meet';
  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,
  34. password });
  35. return;
  36. }
  37. const attachOptions = window.XMPPAttachInfo.data;
  38. if (attachOptions) {
  39. connection.attach(attachOptions);
  40. delete window.XMPPAttachInfo.data;
  41. } else {
  42. connection.connect({ id,
  43. password });
  44. }
  45. } else {
  46. APP.connect.status = 'ready';
  47. APP.connect.handler = checkForAttachParametersAndConnect.bind(null,
  48. id, password, connection);
  49. }
  50. }
  51. /**
  52. * Try to open connection using provided credentials.
  53. * @param {string} [id]
  54. * @param {string} [password]
  55. * @param {string} [roomName]
  56. * @returns {Promise<JitsiConnection>} connection if
  57. * everything is ok, else error.
  58. */
  59. function connect(id, password, roomName) {
  60. const connectionConfig = Object.assign({}, config);
  61. const { issuer, jwt } = APP.store.getState()['features/base/jwt'];
  62. connectionConfig.bosh += `?room=${roomName}`;
  63. const connection
  64. = new JitsiMeetJS.JitsiConnection(
  65. null,
  66. jwt && issuer && issuer !== 'anonymous' ? jwt : undefined,
  67. connectionConfig);
  68. return new Promise((resolve, reject) => {
  69. connection.addEventListener(
  70. JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  71. handleConnectionEstablished);
  72. connection.addEventListener(
  73. JitsiConnectionEvents.CONNECTION_FAILED,
  74. handleConnectionFailed);
  75. connection.addEventListener(
  76. JitsiConnectionEvents.CONNECTION_FAILED,
  77. connectionFailedHandler);
  78. /**
  79. *
  80. */
  81. function connectionFailedHandler(error, message, credentials) {
  82. APP.store.dispatch(
  83. connectionFailed(connection, error, message, credentials));
  84. if (isFatalJitsiConnectionError(error)) {
  85. connection.removeEventListener(
  86. JitsiConnectionEvents.CONNECTION_FAILED,
  87. connectionFailedHandler);
  88. }
  89. }
  90. /**
  91. *
  92. */
  93. function unsubscribe() {
  94. connection.removeEventListener(
  95. JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  96. handleConnectionEstablished);
  97. connection.removeEventListener(
  98. JitsiConnectionEvents.CONNECTION_FAILED,
  99. handleConnectionFailed);
  100. }
  101. /**
  102. *
  103. */
  104. function handleConnectionEstablished() {
  105. APP.store.dispatch(connectionEstablished(connection));
  106. unsubscribe();
  107. resolve(connection);
  108. }
  109. /**
  110. *
  111. */
  112. function handleConnectionFailed(err) {
  113. unsubscribe();
  114. logger.error('CONNECTION FAILED:', err);
  115. reject(err);
  116. }
  117. checkForAttachParametersAndConnect(id, password, connection);
  118. });
  119. }
  120. /**
  121. * Open JitsiConnection using provided credentials.
  122. * If retry option is true it will show auth dialog on PASSWORD_REQUIRED error.
  123. *
  124. * @param {object} options
  125. * @param {string} [options.id]
  126. * @param {string} [options.password]
  127. * @param {string} [options.roomName]
  128. * @param {boolean} [retry] if we should show auth dialog
  129. * on PASSWORD_REQUIRED error.
  130. *
  131. * @returns {Promise<JitsiConnection>}
  132. */
  133. export function openConnection({ id, password, retry, roomName }) {
  134. const usernameOverride
  135. = jitsiLocalStorage.getItem('xmpp_username_override');
  136. const passwordOverride
  137. = jitsiLocalStorage.getItem('xmpp_password_override');
  138. if (usernameOverride && usernameOverride.length > 0) {
  139. id = usernameOverride; // eslint-disable-line no-param-reassign
  140. }
  141. if (passwordOverride && passwordOverride.length > 0) {
  142. password = passwordOverride; // eslint-disable-line no-param-reassign
  143. }
  144. return connect(id, password, roomName).catch(err => {
  145. if (retry) {
  146. const { issuer, jwt } = APP.store.getState()['features/base/jwt'];
  147. if (err === JitsiConnectionErrors.PASSWORD_REQUIRED
  148. && (!jwt || issuer === 'anonymous')) {
  149. return AuthHandler.requestAuth(roomName, connect);
  150. }
  151. }
  152. throw err;
  153. });
  154. }