Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

actions.any.ts 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. import { cloneDeep } from 'lodash-es';
  2. import { IReduxState, IStore } from '../../app/types';
  3. import { conferenceLeft, conferenceWillLeave, redirect } from '../conference/actions';
  4. import { getCurrentConference } from '../conference/functions';
  5. import { IConfigState } from '../config/reducer';
  6. import JitsiMeetJS, { JitsiConnectionEvents } from '../lib-jitsi-meet';
  7. import { parseURLParams } from '../util/parseURLParams';
  8. import {
  9. appendURLParam,
  10. getBackendSafeRoomName
  11. } from '../util/uri';
  12. import {
  13. CONNECTION_DISCONNECTED,
  14. CONNECTION_ESTABLISHED,
  15. CONNECTION_FAILED,
  16. CONNECTION_PROPERTIES_UPDATED,
  17. CONNECTION_WILL_CONNECT,
  18. SET_LOCATION_URL,
  19. SET_PREFER_VISITOR
  20. } from './actionTypes';
  21. import { JITSI_CONNECTION_URL_KEY } from './constants';
  22. import logger from './logger';
  23. import { ConnectionFailedError, IIceServers } from './types';
  24. /**
  25. * The options that will be passed to the JitsiConnection instance.
  26. */
  27. interface IOptions extends IConfigState {
  28. iceServersOverride?: IIceServers;
  29. preferVisitor?: boolean;
  30. }
  31. /**
  32. * Create an action for when the signaling connection has been lost.
  33. *
  34. * @param {JitsiConnection} connection - The {@code JitsiConnection} which
  35. * disconnected.
  36. * @private
  37. * @returns {{
  38. * type: CONNECTION_DISCONNECTED,
  39. * connection: JitsiConnection
  40. * }}
  41. */
  42. export function connectionDisconnected(connection?: Object) {
  43. return {
  44. type: CONNECTION_DISCONNECTED,
  45. connection
  46. };
  47. }
  48. /**
  49. * Create an action for when the signaling connection has been established.
  50. *
  51. * @param {JitsiConnection} connection - The {@code JitsiConnection} which was
  52. * established.
  53. * @param {number} timeEstablished - The time at which the
  54. * {@code JitsiConnection} which was established.
  55. * @public
  56. * @returns {{
  57. * type: CONNECTION_ESTABLISHED,
  58. * connection: JitsiConnection,
  59. * timeEstablished: number
  60. * }}
  61. */
  62. export function connectionEstablished(
  63. connection: Object, timeEstablished: number) {
  64. return {
  65. type: CONNECTION_ESTABLISHED,
  66. connection,
  67. timeEstablished
  68. };
  69. }
  70. /**
  71. * Create an action for when the signaling connection could not be created.
  72. *
  73. * @param {JitsiConnection} connection - The {@code JitsiConnection} which
  74. * failed.
  75. * @param {ConnectionFailedError} error - Error.
  76. * @public
  77. * @returns {{
  78. * type: CONNECTION_FAILED,
  79. * connection: JitsiConnection,
  80. * error: ConnectionFailedError
  81. * }}
  82. */
  83. export function connectionFailed(
  84. connection: Object,
  85. error: ConnectionFailedError) {
  86. const { credentials } = error;
  87. if (credentials && !Object.keys(credentials).length) {
  88. error.credentials = undefined;
  89. }
  90. return {
  91. type: CONNECTION_FAILED,
  92. connection,
  93. error
  94. };
  95. }
  96. /**
  97. * Constructs options to be passed to the constructor of {@code JitsiConnection}
  98. * based on the redux state.
  99. *
  100. * @param {Object} state - The redux state.
  101. * @returns {Object} The options to be passed to the constructor of
  102. * {@code JitsiConnection}.
  103. */
  104. export function constructOptions(state: IReduxState) {
  105. // Deep clone the options to make sure we don't modify the object in the
  106. // redux store.
  107. const options: IOptions = cloneDeep(state['features/base/config']);
  108. const { locationURL, preferVisitor } = state['features/base/connection'];
  109. const params = parseURLParams(locationURL || '');
  110. const iceServersOverride = params['iceServers.replace'];
  111. if (iceServersOverride) {
  112. options.iceServersOverride = iceServersOverride;
  113. }
  114. const { bosh, preferBosh, flags } = options;
  115. let { websocket } = options;
  116. if (preferBosh) {
  117. websocket = undefined;
  118. }
  119. // WebSocket is preferred over BOSH.
  120. const serviceUrl = websocket || bosh;
  121. logger.log(`Using service URL ${serviceUrl}`);
  122. // Append room to the URL's search.
  123. const { room } = state['features/base/conference'];
  124. if (serviceUrl && room) {
  125. const roomName = getBackendSafeRoomName(room);
  126. options.serviceUrl = appendURLParam(serviceUrl, 'room', roomName ?? '');
  127. if (options.websocketKeepAliveUrl) {
  128. options.websocketKeepAliveUrl = appendURLParam(options.websocketKeepAliveUrl, 'room', roomName ?? '');
  129. }
  130. if (options.conferenceRequestUrl) {
  131. options.conferenceRequestUrl = appendURLParam(options.conferenceRequestUrl, 'room', roomName ?? '');
  132. }
  133. }
  134. if (preferVisitor) {
  135. options.preferVisitor = true;
  136. }
  137. // Enable ssrc-rewriting by default.
  138. if (typeof flags?.ssrcRewritingEnabled === 'undefined') {
  139. const { ...otherFlags } = flags ?? {};
  140. options.flags = {
  141. ...otherFlags,
  142. ssrcRewritingEnabled: true
  143. };
  144. }
  145. return options;
  146. }
  147. /**
  148. * Sets the location URL of the application, connection, conference, etc.
  149. *
  150. * @param {URL} [locationURL] - The location URL of the application,
  151. * connection, conference, etc.
  152. * @returns {{
  153. * type: SET_LOCATION_URL,
  154. * locationURL: URL
  155. * }}
  156. */
  157. export function setLocationURL(locationURL?: URL) {
  158. return {
  159. type: SET_LOCATION_URL,
  160. locationURL
  161. };
  162. }
  163. /**
  164. * To change prefer visitor in the store. Used later to decide what to request from jicofo on connection.
  165. *
  166. * @param {boolean} preferVisitor - The value to set.
  167. * @returns {{
  168. * type: SET_PREFER_VISITOR,
  169. * preferVisitor: boolean
  170. * }}
  171. */
  172. export function setPreferVisitor(preferVisitor: boolean) {
  173. return {
  174. type: SET_PREFER_VISITOR,
  175. preferVisitor
  176. };
  177. }
  178. /**
  179. * Opens new connection.
  180. *
  181. * @param {string} [id] - The XMPP user's ID (e.g. {@code user@server.com}).
  182. * @param {string} [password] - The XMPP user's password.
  183. * @returns {Function}
  184. */
  185. export function _connectInternal(id?: string, password?: string) {
  186. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  187. const state = getState();
  188. const options = constructOptions(state);
  189. const { locationURL } = state['features/base/connection'];
  190. const { jwt } = state['features/base/jwt'];
  191. const connection = new JitsiMeetJS.JitsiConnection(options.appId, jwt, options);
  192. connection[JITSI_CONNECTION_URL_KEY] = locationURL;
  193. dispatch(_connectionWillConnect(connection));
  194. return new Promise((resolve, reject) => {
  195. connection.addEventListener(
  196. JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  197. _onConnectionDisconnected);
  198. connection.addEventListener(
  199. JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  200. _onConnectionEstablished);
  201. connection.addEventListener(
  202. JitsiConnectionEvents.CONNECTION_FAILED,
  203. _onConnectionFailed);
  204. connection.addEventListener(
  205. JitsiConnectionEvents.CONNECTION_REDIRECTED,
  206. _onConnectionRedirected);
  207. connection.addEventListener(
  208. JitsiConnectionEvents.PROPERTIES_UPDATED,
  209. _onPropertiesUpdate);
  210. /**
  211. * Unsubscribe the connection instance from
  212. * {@code CONNECTION_DISCONNECTED} and {@code CONNECTION_FAILED} events.
  213. *
  214. * @returns {void}
  215. */
  216. function unsubscribe() {
  217. connection.removeEventListener(
  218. JitsiConnectionEvents.CONNECTION_DISCONNECTED, _onConnectionDisconnected);
  219. connection.removeEventListener(JitsiConnectionEvents.CONNECTION_FAILED, _onConnectionFailed);
  220. connection.removeEventListener(JitsiConnectionEvents.PROPERTIES_UPDATED, _onPropertiesUpdate);
  221. }
  222. /**
  223. * Dispatches {@code CONNECTION_DISCONNECTED} action when connection is
  224. * disconnected.
  225. *
  226. * @private
  227. * @returns {void}
  228. */
  229. function _onConnectionDisconnected() {
  230. unsubscribe();
  231. dispatch(connectionDisconnected(connection));
  232. resolve(connection);
  233. }
  234. /**
  235. * Rejects external promise when connection fails.
  236. *
  237. * @param {JitsiConnectionErrors} err - Connection error.
  238. * @param {string} [message] - Error message supplied by lib-jitsi-meet.
  239. * @param {Object} [credentials] - The invalid credentials that were
  240. * used to authenticate and the authentication failed.
  241. * @param {string} [credentials.jid] - The XMPP user's ID.
  242. * @param {string} [credentials.password] - The XMPP user's password.
  243. * @param {Object} details - Additional information about the error.
  244. * @private
  245. * @returns {void}
  246. */
  247. function _onConnectionFailed( // eslint-disable-line max-params
  248. err: string,
  249. message: string,
  250. credentials: any,
  251. details: Object) {
  252. unsubscribe();
  253. dispatch(connectionFailed(connection, {
  254. credentials,
  255. details,
  256. name: err,
  257. message
  258. }));
  259. reject(err);
  260. }
  261. /**
  262. * Resolves external promise when connection is established.
  263. *
  264. * @private
  265. * @returns {void}
  266. */
  267. function _onConnectionEstablished() {
  268. connection.removeEventListener(JitsiConnectionEvents.CONNECTION_ESTABLISHED, _onConnectionEstablished);
  269. dispatch(connectionEstablished(connection, Date.now()));
  270. resolve(connection);
  271. }
  272. /**
  273. * Connection was redirected.
  274. *
  275. * @param {string|undefined} vnode - The vnode to connect to.
  276. * @param {string} focusJid - The focus jid to use.
  277. * @param {string|undefined} username - The username to use when joining. This is after promotion from
  278. * visitor to main participant.
  279. * @private
  280. * @returns {void}
  281. */
  282. function _onConnectionRedirected(vnode: string, focusJid: string, username: string) {
  283. connection.removeEventListener(JitsiConnectionEvents.CONNECTION_REDIRECTED, _onConnectionRedirected);
  284. dispatch(redirect(vnode, focusJid, username));
  285. }
  286. /**
  287. * Connection properties were updated.
  288. *
  289. * @param {Object} properties - The properties which were updated.
  290. * @private
  291. * @returns {void}
  292. */
  293. function _onPropertiesUpdate(properties: object) {
  294. dispatch(_propertiesUpdate(properties));
  295. }
  296. // in case of configured http url for conference request we need the room name
  297. const name = getBackendSafeRoomName(state['features/base/conference'].room);
  298. connection.connect({
  299. id,
  300. password,
  301. name
  302. });
  303. });
  304. };
  305. }
  306. /**
  307. * Create an action for when a connection will connect.
  308. *
  309. * @param {JitsiConnection} connection - The {@code JitsiConnection} which will
  310. * connect.
  311. * @private
  312. * @returns {{
  313. * type: CONNECTION_WILL_CONNECT,
  314. * connection: JitsiConnection
  315. * }}
  316. */
  317. function _connectionWillConnect(connection: Object) {
  318. return {
  319. type: CONNECTION_WILL_CONNECT,
  320. connection
  321. };
  322. }
  323. /**
  324. * Create an action for when connection properties are updated.
  325. *
  326. * @param {Object} properties - The properties which were updated.
  327. * @private
  328. * @returns {{
  329. * type: CONNECTION_PROPERTIES_UPDATED,
  330. * properties: Object
  331. * }}
  332. */
  333. function _propertiesUpdate(properties: object) {
  334. return {
  335. type: CONNECTION_PROPERTIES_UPDATED,
  336. properties
  337. };
  338. }
  339. /**
  340. * Closes connection.
  341. *
  342. * @param {boolean} isRedirect - Indicates if the action has been dispatched as part of visitor promotion.
  343. *
  344. * @returns {Function}
  345. */
  346. export function disconnect(isRedirect?: boolean) {
  347. return (dispatch: IStore['dispatch'], getState: IStore['getState']): Promise<void> => {
  348. const state = getState();
  349. // The conference we have already joined or are joining.
  350. const conference_ = getCurrentConference(state);
  351. // Promise which completes when the conference has been left and the
  352. // connection has been disconnected.
  353. let promise;
  354. // Leave the conference.
  355. if (conference_) {
  356. // In a fashion similar to JitsiConference's CONFERENCE_LEFT event
  357. // (and the respective Redux action) which is fired after the
  358. // conference has been left, notify the application about the
  359. // intention to leave the conference.
  360. dispatch(conferenceWillLeave(conference_, isRedirect));
  361. promise
  362. = conference_.leave()
  363. .catch((error: Error) => {
  364. logger.warn(
  365. 'JitsiConference.leave() rejected with:',
  366. error);
  367. // The library lib-jitsi-meet failed to make the
  368. // JitsiConference leave. Which may be because
  369. // JitsiConference thinks it has already left.
  370. // Regardless of the failure reason, continue in
  371. // jitsi-meet as if the leave has succeeded.
  372. dispatch(conferenceLeft(conference_));
  373. });
  374. } else {
  375. promise = Promise.resolve();
  376. }
  377. // Disconnect the connection.
  378. const { connecting, connection } = state['features/base/connection'];
  379. // The connection we have already connected or are connecting.
  380. const connection_ = connection || connecting;
  381. if (connection_) {
  382. promise = promise.then(() => connection_.disconnect());
  383. } else {
  384. logger.info('No connection found while disconnecting.');
  385. }
  386. return promise;
  387. };
  388. }