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

actions.native.js 9.5KB

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