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

actions.native.js 11KB

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