Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

connection.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* global APP, JitsiMeetJS, config */
  2. import Logger from 'jitsi-meet-logger';
  3. import { jitsiLocalStorage } from 'js-utils';
  4. import AuthHandler from './modules/UI/authentication/AuthHandler';
  5. import {
  6. connectionEstablished,
  7. connectionFailed
  8. } from './react/features/base/connection';
  9. import {
  10. isFatalJitsiConnectionError,
  11. JitsiConnectionErrors,
  12. JitsiConnectionEvents
  13. } from './react/features/base/lib-jitsi-meet';
  14. const logger = Logger.getLogger(__filename);
  15. /**
  16. * The feature announced so we can distinguish jibri participants.
  17. *
  18. * @type {string}
  19. */
  20. export const DISCO_JIBRI_FEATURE = 'http://jitsi.org/protocol/jibri';
  21. /**
  22. * Checks if we have data to use attach instead of connect. If we have the data
  23. * executes attach otherwise check if we have to wait for the data. If we have
  24. * to wait for the attach data we are setting handler to APP.connect.handler
  25. * which is going to be called when the attach data is received otherwise
  26. * executes connect.
  27. *
  28. * @param {string} [id] user id
  29. * @param {string} [password] password
  30. * @param {string} [roomName] the name of the conference.
  31. */
  32. function checkForAttachParametersAndConnect(id, password, connection) {
  33. if (window.XMPPAttachInfo) {
  34. APP.connect.status = 'connecting';
  35. // When connection optimization is not deployed or enabled the default
  36. // value will be window.XMPPAttachInfo.status = "error"
  37. // If the connection optimization is deployed and enabled and there is
  38. // a failure the value will be window.XMPPAttachInfo.status = "error"
  39. if (window.XMPPAttachInfo.status === 'error') {
  40. connection.connect({
  41. id,
  42. password
  43. });
  44. return;
  45. }
  46. const attachOptions = window.XMPPAttachInfo.data;
  47. if (attachOptions) {
  48. connection.attach(attachOptions);
  49. delete window.XMPPAttachInfo.data;
  50. } else {
  51. connection.connect({
  52. id,
  53. password
  54. });
  55. }
  56. } else {
  57. APP.connect.status = 'ready';
  58. APP.connect.handler
  59. = checkForAttachParametersAndConnect.bind(
  60. null,
  61. id, password, connection);
  62. }
  63. }
  64. /**
  65. * Try to open connection using provided credentials.
  66. * @param {string} [id]
  67. * @param {string} [password]
  68. * @param {string} [roomName]
  69. * @returns {Promise<JitsiConnection>} connection if
  70. * everything is ok, else error.
  71. */
  72. function connect(id, password, roomName) {
  73. const connectionConfig = Object.assign({}, config);
  74. const { issuer, jwt } = APP.store.getState()['features/base/jwt'];
  75. // Use Websocket URL for the web app if configured. Note that there is no 'isWeb' check, because there's assumption
  76. // that this code executes only on web browsers/electron. This needs to be changed when mobile and web are unified.
  77. let serviceUrl = connectionConfig.websocket || connectionConfig.bosh;
  78. serviceUrl += `?room=${roomName}`;
  79. // FIXME Remove deprecated 'bosh' option assignment at some point(LJM will be accepting only 'serviceUrl' option
  80. // in future). It's included for the time being for Jitsi Meet and lib-jitsi-meet versions interoperability.
  81. connectionConfig.serviceUrl = connectionConfig.bosh = serviceUrl;
  82. const connection
  83. = new JitsiMeetJS.JitsiConnection(
  84. null,
  85. jwt && issuer && issuer !== 'anonymous' ? jwt : undefined,
  86. connectionConfig);
  87. if (config.iAmRecorder) {
  88. connection.addFeature(DISCO_JIBRI_FEATURE);
  89. }
  90. return new Promise((resolve, reject) => {
  91. connection.addEventListener(
  92. JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  93. handleConnectionEstablished);
  94. connection.addEventListener(
  95. JitsiConnectionEvents.CONNECTION_FAILED,
  96. handleConnectionFailed);
  97. connection.addEventListener(
  98. JitsiConnectionEvents.CONNECTION_FAILED,
  99. connectionFailedHandler);
  100. /* eslint-disable max-params */
  101. /**
  102. *
  103. */
  104. function connectionFailedHandler(error, message, credentials, details) {
  105. /* eslint-enable max-params */
  106. APP.store.dispatch(
  107. connectionFailed(
  108. connection, {
  109. credentials,
  110. details,
  111. message,
  112. name: error
  113. }));
  114. if (isFatalJitsiConnectionError(error)) {
  115. connection.removeEventListener(
  116. JitsiConnectionEvents.CONNECTION_FAILED,
  117. connectionFailedHandler);
  118. }
  119. }
  120. /**
  121. *
  122. */
  123. function unsubscribe() {
  124. connection.removeEventListener(
  125. JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  126. handleConnectionEstablished);
  127. connection.removeEventListener(
  128. JitsiConnectionEvents.CONNECTION_FAILED,
  129. handleConnectionFailed);
  130. }
  131. /**
  132. *
  133. */
  134. function handleConnectionEstablished() {
  135. APP.store.dispatch(connectionEstablished(connection, Date.now()));
  136. unsubscribe();
  137. resolve(connection);
  138. }
  139. /**
  140. *
  141. */
  142. function handleConnectionFailed(err) {
  143. unsubscribe();
  144. logger.error('CONNECTION FAILED:', err);
  145. reject(err);
  146. }
  147. checkForAttachParametersAndConnect(id, password, connection);
  148. });
  149. }
  150. /**
  151. * Open JitsiConnection using provided credentials.
  152. * If retry option is true it will show auth dialog on PASSWORD_REQUIRED error.
  153. *
  154. * @param {object} options
  155. * @param {string} [options.id]
  156. * @param {string} [options.password]
  157. * @param {string} [options.roomName]
  158. * @param {boolean} [retry] if we should show auth dialog
  159. * on PASSWORD_REQUIRED error.
  160. *
  161. * @returns {Promise<JitsiConnection>}
  162. */
  163. export function openConnection({ id, password, retry, roomName }) {
  164. const usernameOverride
  165. = jitsiLocalStorage.getItem('xmpp_username_override');
  166. const passwordOverride
  167. = jitsiLocalStorage.getItem('xmpp_password_override');
  168. if (usernameOverride && usernameOverride.length > 0) {
  169. id = usernameOverride; // eslint-disable-line no-param-reassign
  170. }
  171. if (passwordOverride && passwordOverride.length > 0) {
  172. password = passwordOverride; // eslint-disable-line no-param-reassign
  173. }
  174. return connect(id, password, roomName).catch(err => {
  175. if (retry) {
  176. const { issuer, jwt } = APP.store.getState()['features/base/jwt'];
  177. if (err === JitsiConnectionErrors.PASSWORD_REQUIRED
  178. && (!jwt || issuer === 'anonymous')) {
  179. return AuthHandler.requestAuth(roomName, connect);
  180. }
  181. }
  182. throw err;
  183. });
  184. }