Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import { IStore } from '../../app/types';
  2. import { conferenceLeft, conferenceWillLeave } from '../conference/actions';
  3. import { getCurrentConference } from '../conference/functions';
  4. import JitsiMeetJS, { JitsiConnectionEvents } from '../lib-jitsi-meet';
  5. import {
  6. CONNECTION_WILL_CONNECT
  7. } from './actionTypes';
  8. import {
  9. connectionDisconnected,
  10. connectionEstablished,
  11. connectionFailed,
  12. constructOptions
  13. } from './actions.any';
  14. import { JITSI_CONNECTION_URL_KEY } from './constants';
  15. import logger from './logger';
  16. export * from './actions.any';
  17. /**
  18. * Opens new connection.
  19. *
  20. * @param {string} [id] - The XMPP user's ID (e.g. {@code user@server.com}).
  21. * @param {string} [password] - The XMPP user's password.
  22. * @returns {Function}
  23. */
  24. export function connect(id?: string, password?: string) {
  25. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  26. const state = getState();
  27. const options = constructOptions(state);
  28. const { locationURL } = state['features/base/connection'];
  29. const { jwt } = state['features/base/jwt'];
  30. const connection = new JitsiMeetJS.JitsiConnection(options.appId, jwt, options);
  31. connection[JITSI_CONNECTION_URL_KEY] = locationURL;
  32. dispatch(_connectionWillConnect(connection));
  33. connection.addEventListener(
  34. JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  35. _onConnectionDisconnected);
  36. connection.addEventListener(
  37. JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  38. _onConnectionEstablished);
  39. connection.addEventListener(
  40. JitsiConnectionEvents.CONNECTION_FAILED,
  41. _onConnectionFailed);
  42. connection.connect({
  43. id,
  44. password
  45. });
  46. /**
  47. * Dispatches {@code CONNECTION_DISCONNECTED} action when connection is
  48. * disconnected.
  49. *
  50. * @private
  51. * @returns {void}
  52. */
  53. function _onConnectionDisconnected() {
  54. unsubscribe();
  55. dispatch(connectionDisconnected(connection));
  56. }
  57. /**
  58. * Resolves external promise when connection is established.
  59. *
  60. * @private
  61. * @returns {void}
  62. */
  63. function _onConnectionEstablished() {
  64. connection.removeEventListener(
  65. JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  66. _onConnectionEstablished);
  67. dispatch(connectionEstablished(connection, Date.now()));
  68. }
  69. /**
  70. * Rejects external promise when connection fails.
  71. *
  72. * @param {JitsiConnectionErrors} err - Connection error.
  73. * @param {string} [msg] - Error message supplied by lib-jitsi-meet.
  74. * @param {Object} [credentials] - The invalid credentials that were
  75. * used to authenticate and the authentication failed.
  76. * @param {string} [credentials.jid] - The XMPP user's ID.
  77. * @param {string} [credentials.password] - The XMPP user's password.
  78. * @param {Object} details - Additional information about the error.
  79. * @private
  80. * @returns {void}
  81. */
  82. function _onConnectionFailed( // eslint-disable-line max-params
  83. err: string,
  84. msg: string,
  85. credentials: any,
  86. details: Object) {
  87. unsubscribe();
  88. dispatch(
  89. connectionFailed(
  90. connection, {
  91. credentials,
  92. details,
  93. name: err,
  94. message: msg
  95. }
  96. ));
  97. }
  98. /**
  99. * Unsubscribe the connection instance from
  100. * {@code CONNECTION_DISCONNECTED} and {@code CONNECTION_FAILED} events.
  101. *
  102. * @returns {void}
  103. */
  104. function unsubscribe() {
  105. connection.removeEventListener(
  106. JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  107. _onConnectionDisconnected);
  108. connection.removeEventListener(
  109. JitsiConnectionEvents.CONNECTION_FAILED,
  110. _onConnectionFailed);
  111. }
  112. };
  113. }
  114. /**
  115. * Create an action for when a connection will connect.
  116. *
  117. * @param {JitsiConnection} connection - The {@code JitsiConnection} which will
  118. * connect.
  119. * @private
  120. * @returns {{
  121. * type: CONNECTION_WILL_CONNECT,
  122. * connection: JitsiConnection
  123. * }}
  124. */
  125. function _connectionWillConnect(connection: Object) {
  126. return {
  127. type: CONNECTION_WILL_CONNECT,
  128. connection
  129. };
  130. }
  131. /* eslint-disable @typescript-eslint/no-unused-vars */
  132. /**
  133. * Closes connection.
  134. *
  135. * @param {boolean} _ - Used in web.
  136. * @returns {Function}
  137. */
  138. export function disconnect(_?: boolean) {
  139. /* eslint-enable @typescript-eslint/no-unused-vars */
  140. return (dispatch: IStore['dispatch'], getState: IStore['getState']): Promise<void> => {
  141. const state = getState();
  142. // The conference we have already joined or are joining.
  143. const conference_ = getCurrentConference(state);
  144. // Promise which completes when the conference has been left and the
  145. // connection has been disconnected.
  146. let promise;
  147. // Leave the conference.
  148. if (conference_) {
  149. // In a fashion similar to JitsiConference's CONFERENCE_LEFT event
  150. // (and the respective Redux action) which is fired after the
  151. // conference has been left, notify the application about the
  152. // intention to leave the conference.
  153. dispatch(conferenceWillLeave(conference_));
  154. promise
  155. = conference_.leave()
  156. .catch((error: Error) => {
  157. logger.warn(
  158. 'JitsiConference.leave() rejected with:',
  159. error);
  160. // The library lib-jitsi-meet failed to make the
  161. // JitsiConference leave. Which may be because
  162. // JitsiConference thinks it has already left.
  163. // Regardless of the failure reason, continue in
  164. // jitsi-meet as if the leave has succeeded.
  165. dispatch(conferenceLeft(conference_));
  166. });
  167. } else {
  168. promise = Promise.resolve();
  169. }
  170. // Disconnect the connection.
  171. const { connecting, connection } = state['features/base/connection'];
  172. // The connection we have already connected or are connecting.
  173. const connection_ = connection || connecting;
  174. if (connection_) {
  175. promise = promise.then(() => connection_.disconnect());
  176. } else {
  177. logger.info('No connection found while disconnecting.');
  178. }
  179. return promise;
  180. };
  181. }