Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

connection.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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({
  34. id,
  35. password
  36. });
  37. return;
  38. }
  39. const attachOptions = window.XMPPAttachInfo.data;
  40. if (attachOptions) {
  41. connection.attach(attachOptions);
  42. delete window.XMPPAttachInfo.data;
  43. } else {
  44. connection.connect({
  45. id,
  46. password
  47. });
  48. }
  49. } else {
  50. APP.connect.status = 'ready';
  51. APP.connect.handler
  52. = checkForAttachParametersAndConnect.bind(
  53. null,
  54. id, password, connection);
  55. }
  56. }
  57. /**
  58. * Try to open connection using provided credentials.
  59. * @param {string} [id]
  60. * @param {string} [password]
  61. * @param {string} [roomName]
  62. * @returns {Promise<JitsiConnection>} connection if
  63. * everything is ok, else error.
  64. */
  65. function connect(id, password, roomName) {
  66. const connectionConfig = Object.assign({}, config);
  67. const { issuer, jwt } = APP.store.getState()['features/base/jwt'];
  68. // Use Websocket URL for the web app if configured. Note that there is no 'isWeb' check, because there's assumption
  69. // that this code executes only on web browsers/electron. This needs to be changed when mobile and web are unified.
  70. let serviceUrl = connectionConfig.websocket || connectionConfig.bosh;
  71. serviceUrl += `?room=${roomName}`;
  72. // FIXME Remove deprecated 'bosh' option assignment at some point(LJM will be accepting only 'serviceUrl' option
  73. // in future). It's included for the time being for Jitsi Meet and lib-jitsi-meet versions interoperability.
  74. connectionConfig.serviceUrl = connectionConfig.bosh = serviceUrl;
  75. const connection
  76. = new JitsiMeetJS.JitsiConnection(
  77. null,
  78. jwt && issuer && issuer !== 'anonymous' ? jwt : undefined,
  79. connectionConfig);
  80. return new Promise((resolve, reject) => {
  81. connection.addEventListener(
  82. JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  83. handleConnectionEstablished);
  84. connection.addEventListener(
  85. JitsiConnectionEvents.CONNECTION_FAILED,
  86. handleConnectionFailed);
  87. connection.addEventListener(
  88. JitsiConnectionEvents.CONNECTION_FAILED,
  89. connectionFailedHandler);
  90. /* eslint-disable max-params */
  91. /**
  92. *
  93. */
  94. function connectionFailedHandler(error, message, credentials, details) {
  95. /* eslint-enable max-params */
  96. APP.store.dispatch(
  97. connectionFailed(
  98. connection, {
  99. credentials,
  100. details,
  101. message,
  102. name: error
  103. }));
  104. if (isFatalJitsiConnectionError(error)) {
  105. connection.removeEventListener(
  106. JitsiConnectionEvents.CONNECTION_FAILED,
  107. connectionFailedHandler);
  108. }
  109. }
  110. /**
  111. *
  112. */
  113. function unsubscribe() {
  114. connection.removeEventListener(
  115. JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  116. handleConnectionEstablished);
  117. connection.removeEventListener(
  118. JitsiConnectionEvents.CONNECTION_FAILED,
  119. handleConnectionFailed);
  120. }
  121. /**
  122. *
  123. */
  124. function handleConnectionEstablished() {
  125. APP.store.dispatch(connectionEstablished(connection, Date.now()));
  126. unsubscribe();
  127. resolve(connection);
  128. }
  129. /**
  130. *
  131. */
  132. function handleConnectionFailed(err) {
  133. unsubscribe();
  134. logger.error('CONNECTION FAILED:', err);
  135. reject(err);
  136. }
  137. checkForAttachParametersAndConnect(id, password, connection);
  138. });
  139. }
  140. /**
  141. * Open JitsiConnection using provided credentials.
  142. * If retry option is true it will show auth dialog on PASSWORD_REQUIRED error.
  143. *
  144. * @param {object} options
  145. * @param {string} [options.id]
  146. * @param {string} [options.password]
  147. * @param {string} [options.roomName]
  148. * @param {boolean} [retry] if we should show auth dialog
  149. * on PASSWORD_REQUIRED error.
  150. *
  151. * @returns {Promise<JitsiConnection>}
  152. */
  153. export function openConnection({ id, password, retry, roomName }) {
  154. const usernameOverride
  155. = jitsiLocalStorage.getItem('xmpp_username_override');
  156. const passwordOverride
  157. = jitsiLocalStorage.getItem('xmpp_password_override');
  158. if (usernameOverride && usernameOverride.length > 0) {
  159. id = usernameOverride; // eslint-disable-line no-param-reassign
  160. }
  161. if (passwordOverride && passwordOverride.length > 0) {
  162. password = passwordOverride; // eslint-disable-line no-param-reassign
  163. }
  164. return connect(id, password, roomName).catch(err => {
  165. if (retry) {
  166. const { issuer, jwt } = APP.store.getState()['features/base/jwt'];
  167. if (err === JitsiConnectionErrors.PASSWORD_REQUIRED
  168. && (!jwt || issuer === 'anonymous')) {
  169. return AuthHandler.requestAuth(roomName, connect);
  170. }
  171. }
  172. throw err;
  173. });
  174. }