Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

connection.js 8.6KB

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