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.

actions.native.ts 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. /**
  132. * Closes connection.
  133. *
  134. * @param {boolean} _ - Used in web.
  135. * @returns {Function}
  136. */
  137. export function disconnect(_?: boolean) {
  138. /* eslint-enable @typescript-eslint/no-unused-vars */
  139. return (dispatch: IStore['dispatch'], getState: IStore['getState']): Promise<void> => {
  140. const state = getState();
  141. // The conference we have already joined or are joining.
  142. const conference_ = getCurrentConference(state);
  143. // Promise which completes when the conference has been left and the
  144. // connection has been disconnected.
  145. let promise;
  146. // Leave the conference.
  147. if (conference_) {
  148. // In a fashion similar to JitsiConference's CONFERENCE_LEFT event
  149. // (and the respective Redux action) which is fired after the
  150. // conference has been left, notify the application about the
  151. // intention to leave the conference.
  152. dispatch(conferenceWillLeave(conference_));
  153. promise
  154. = conference_.leave()
  155. .catch((error: Error) => {
  156. logger.warn(
  157. 'JitsiConference.leave() rejected with:',
  158. error);
  159. // The library lib-jitsi-meet failed to make the
  160. // JitsiConference leave. Which may be because
  161. // JitsiConference thinks it has already left.
  162. // Regardless of the failure reason, continue in
  163. // jitsi-meet as if the leave has succeeded.
  164. dispatch(conferenceLeft(conference_));
  165. });
  166. } else {
  167. promise = Promise.resolve();
  168. }
  169. // Disconnect the connection.
  170. const { connecting, connection } = state['features/base/connection'];
  171. // The connection we have already connected or are connecting.
  172. const connection_ = connection || connecting;
  173. if (connection_) {
  174. promise = promise.then(() => connection_.disconnect());
  175. } else {
  176. logger.info('No connection found while disconnecting.');
  177. }
  178. return promise;
  179. };
  180. }