您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

actions.native.ts 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* eslint-disable lines-around-comment */
  2. import { Dispatch } from 'redux';
  3. // @ts-ignore
  4. import { conferenceLeft, conferenceWillLeave } from '../conference/actions';
  5. import { getCurrentConference } from '../conference/functions';
  6. import JitsiMeetJS, { JitsiConnectionEvents } from '../lib-jitsi-meet';
  7. import {
  8. CONNECTION_DISCONNECTED,
  9. CONNECTION_ESTABLISHED,
  10. CONNECTION_FAILED,
  11. CONNECTION_WILL_CONNECT,
  12. SET_LOCATION_URL
  13. } from './actionTypes';
  14. import { constructOptions } from './actions.any';
  15. import { JITSI_CONNECTION_URL_KEY } from './constants';
  16. import logger from './logger';
  17. export * from './actions.any';
  18. /**
  19. * The error structure passed to the {@link connectionFailed} action.
  20. *
  21. * Note there was an intention to make the error resemble an Error instance (to
  22. * the extent that jitsi-meet needs it).
  23. */
  24. export type ConnectionFailedError = {
  25. /**
  26. * The invalid credentials that were used to authenticate and the
  27. * authentication failed.
  28. */
  29. credentials?: {
  30. /**
  31. * The XMPP user's ID.
  32. */
  33. jid: string;
  34. /**
  35. * The XMPP user's password.
  36. */
  37. password: string;
  38. };
  39. /**
  40. * The details about the connection failed event.
  41. */
  42. details?: Object;
  43. /**
  44. * Error message.
  45. */
  46. message?: string;
  47. /**
  48. * One of {@link JitsiConnectionError} constants (defined in
  49. * lib-jitsi-meet).
  50. */
  51. name: string;
  52. /**
  53. * Indicates whether this event is recoverable or not.
  54. */
  55. recoverable?: boolean;
  56. };
  57. /**
  58. * Opens new connection.
  59. *
  60. * @param {string} [id] - The XMPP user's ID (e.g. {@code user@server.com}).
  61. * @param {string} [password] - The XMPP user's password.
  62. * @returns {Function}
  63. */
  64. export function connect(id?: string, password?: string) {
  65. return (dispatch: Dispatch<any>, getState: Function) => {
  66. const state = getState();
  67. const options = constructOptions(state);
  68. const { locationURL } = state['features/base/connection'];
  69. const { jwt } = state['features/base/jwt'];
  70. const connection = new JitsiMeetJS.JitsiConnection(options.appId, jwt, options);
  71. connection[JITSI_CONNECTION_URL_KEY] = locationURL;
  72. dispatch(_connectionWillConnect(connection));
  73. connection.addEventListener(
  74. JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  75. _onConnectionDisconnected);
  76. connection.addEventListener(
  77. JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  78. _onConnectionEstablished);
  79. connection.addEventListener(
  80. JitsiConnectionEvents.CONNECTION_FAILED,
  81. _onConnectionFailed);
  82. connection.connect({
  83. id,
  84. password
  85. });
  86. /**
  87. * Dispatches {@code CONNECTION_DISCONNECTED} action when connection is
  88. * disconnected.
  89. *
  90. * @private
  91. * @returns {void}
  92. */
  93. function _onConnectionDisconnected() {
  94. unsubscribe();
  95. dispatch(connectionDisconnected(connection));
  96. }
  97. /**
  98. * Resolves external promise when connection is established.
  99. *
  100. * @private
  101. * @returns {void}
  102. */
  103. function _onConnectionEstablished() {
  104. connection.removeEventListener(
  105. JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  106. _onConnectionEstablished);
  107. dispatch(connectionEstablished(connection, Date.now()));
  108. }
  109. /**
  110. * Rejects external promise when connection fails.
  111. *
  112. * @param {JitsiConnectionErrors} err - Connection error.
  113. * @param {string} [msg] - Error message supplied by lib-jitsi-meet.
  114. * @param {Object} [credentials] - The invalid credentials that were
  115. * used to authenticate and the authentication failed.
  116. * @param {string} [credentials.jid] - The XMPP user's ID.
  117. * @param {string} [credentials.password] - The XMPP user's password.
  118. * @param {Object} details - Additional information about the error.
  119. * @private
  120. * @returns {void}
  121. */
  122. function _onConnectionFailed( // eslint-disable-line max-params
  123. err: string,
  124. msg: string,
  125. credentials: any,
  126. details: Object) {
  127. unsubscribe();
  128. dispatch(
  129. connectionFailed(
  130. connection, {
  131. credentials,
  132. details,
  133. name: err,
  134. message: msg
  135. }
  136. ));
  137. }
  138. /**
  139. * Unsubscribe the connection instance from
  140. * {@code CONNECTION_DISCONNECTED} and {@code CONNECTION_FAILED} events.
  141. *
  142. * @returns {void}
  143. */
  144. function unsubscribe() {
  145. connection.removeEventListener(
  146. JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  147. _onConnectionDisconnected);
  148. connection.removeEventListener(
  149. JitsiConnectionEvents.CONNECTION_FAILED,
  150. _onConnectionFailed);
  151. }
  152. };
  153. }
  154. /**
  155. * Create an action for when the signaling connection has been lost.
  156. *
  157. * @param {JitsiConnection} connection - The {@code JitsiConnection} which
  158. * disconnected.
  159. * @private
  160. * @returns {{
  161. * type: CONNECTION_DISCONNECTED,
  162. * connection: JitsiConnection
  163. * }}
  164. */
  165. export function connectionDisconnected(connection: Object) {
  166. return {
  167. type: CONNECTION_DISCONNECTED,
  168. connection
  169. };
  170. }
  171. /**
  172. * Create an action for when the signaling connection has been established.
  173. *
  174. * @param {JitsiConnection} connection - The {@code JitsiConnection} which was
  175. * established.
  176. * @param {number} timeEstablished - The time at which the
  177. * {@code JitsiConnection} which was established.
  178. * @public
  179. * @returns {{
  180. * type: CONNECTION_ESTABLISHED,
  181. * connection: JitsiConnection,
  182. * timeEstablished: number
  183. * }}
  184. */
  185. export function connectionEstablished(
  186. connection: Object, timeEstablished: number) {
  187. return {
  188. type: CONNECTION_ESTABLISHED,
  189. connection,
  190. timeEstablished
  191. };
  192. }
  193. /**
  194. * Create an action for when the signaling connection could not be created.
  195. *
  196. * @param {JitsiConnection} connection - The {@code JitsiConnection} which
  197. * failed.
  198. * @param {ConnectionFailedError} error - Error.
  199. * @public
  200. * @returns {{
  201. * type: CONNECTION_FAILED,
  202. * connection: JitsiConnection,
  203. * error: ConnectionFailedError
  204. * }}
  205. */
  206. export function connectionFailed(
  207. connection: Object,
  208. error: ConnectionFailedError) {
  209. const { credentials } = error;
  210. if (credentials && !Object.keys(credentials).length) {
  211. error.credentials = undefined;
  212. }
  213. return {
  214. type: CONNECTION_FAILED,
  215. connection,
  216. error
  217. };
  218. }
  219. /**
  220. * Create an action for when a connection will connect.
  221. *
  222. * @param {JitsiConnection} connection - The {@code JitsiConnection} which will
  223. * connect.
  224. * @private
  225. * @returns {{
  226. * type: CONNECTION_WILL_CONNECT,
  227. * connection: JitsiConnection
  228. * }}
  229. */
  230. function _connectionWillConnect(connection: Object) {
  231. return {
  232. type: CONNECTION_WILL_CONNECT,
  233. connection
  234. };
  235. }
  236. /**
  237. * Closes connection.
  238. *
  239. * @returns {Function}
  240. */
  241. export function disconnect() {
  242. return (dispatch: Dispatch<any>, getState: Function): Promise<void> => {
  243. const state = getState();
  244. // The conference we have already joined or are joining.
  245. const conference_ = getCurrentConference(state);
  246. // Promise which completes when the conference has been left and the
  247. // connection has been disconnected.
  248. let promise;
  249. // Leave the conference.
  250. if (conference_) {
  251. // In a fashion similar to JitsiConference's CONFERENCE_LEFT event
  252. // (and the respective Redux action) which is fired after the
  253. // conference has been left, notify the application about the
  254. // intention to leave the conference.
  255. dispatch(conferenceWillLeave(conference_));
  256. promise
  257. = conference_.leave()
  258. .catch((error: Error) => {
  259. logger.warn(
  260. 'JitsiConference.leave() rejected with:',
  261. error);
  262. // The library lib-jitsi-meet failed to make the
  263. // JitsiConference leave. Which may be because
  264. // JitsiConference thinks it has already left.
  265. // Regardless of the failure reason, continue in
  266. // jitsi-meet as if the leave has succeeded.
  267. dispatch(conferenceLeft(conference_));
  268. });
  269. } else {
  270. promise = Promise.resolve();
  271. }
  272. // Disconnect the connection.
  273. const { connecting, connection } = state['features/base/connection'];
  274. // The connection we have already connected or are connecting.
  275. const connection_ = connection || connecting;
  276. if (connection_) {
  277. promise = promise.then(() => connection_.disconnect());
  278. } else {
  279. logger.info('No connection found while disconnecting.');
  280. }
  281. return promise;
  282. };
  283. }
  284. /**
  285. * Sets the location URL of the application, connection, conference, etc.
  286. *
  287. * @param {URL} [locationURL] - The location URL of the application,
  288. * connection, conference, etc.
  289. * @returns {{
  290. * type: SET_LOCATION_URL,
  291. * locationURL: URL
  292. * }}
  293. */
  294. export function setLocationURL(locationURL?: URL) {
  295. return {
  296. type: SET_LOCATION_URL,
  297. locationURL
  298. };
  299. }