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

actions.native.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 { parseStandardURIString } 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?: string,
  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. 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<*>, 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));
  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. * @private
  123. * @returns {void}
  124. */
  125. function _onConnectionFailed(
  126. err: string, msg: string, credentials: Object) {
  127. unsubscribe();
  128. dispatch(
  129. connectionFailed(
  130. connection, {
  131. credentials,
  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. * @param {string} message - Error message.
  159. * @private
  160. * @returns {{
  161. * type: CONNECTION_DISCONNECTED,
  162. * connection: JitsiConnection,
  163. * message: string
  164. * }}
  165. */
  166. function _connectionDisconnected(connection: Object, message: string) {
  167. return {
  168. type: CONNECTION_DISCONNECTED,
  169. connection,
  170. message
  171. };
  172. }
  173. /**
  174. * Create an action for when the signaling connection has been established.
  175. *
  176. * @param {JitsiConnection} connection - The {@code JitsiConnection} which was
  177. * established.
  178. * @public
  179. * @returns {{
  180. * type: CONNECTION_ESTABLISHED,
  181. * connection: JitsiConnection
  182. * }}
  183. */
  184. export function connectionEstablished(connection: Object) {
  185. return {
  186. type: CONNECTION_ESTABLISHED,
  187. connection
  188. };
  189. }
  190. /**
  191. * Create an action for when the signaling connection could not be created.
  192. *
  193. * @param {JitsiConnection} connection - The {@code JitsiConnection} which
  194. * failed.
  195. * @param {ConnectionFailedError} error - Error.
  196. * @public
  197. * @returns {{
  198. * type: CONNECTION_FAILED,
  199. * connection: JitsiConnection,
  200. * error: ConnectionFailedError
  201. * }}
  202. */
  203. export function connectionFailed(
  204. connection: Object,
  205. error: ConnectionFailedError) {
  206. const { credentials } = error;
  207. if (credentials && !Object.keys(credentials).length) {
  208. error.credentials = undefined;
  209. }
  210. return {
  211. type: CONNECTION_FAILED,
  212. connection,
  213. error
  214. };
  215. }
  216. /**
  217. * Create an action for when a connection will connect.
  218. *
  219. * @param {JitsiConnection} connection - The {@code JitsiConnection} which will
  220. * connect.
  221. * @private
  222. * @returns {{
  223. * type: CONNECTION_WILL_CONNECT,
  224. * connection: JitsiConnection
  225. * }}
  226. */
  227. function _connectionWillConnect(connection) {
  228. return {
  229. type: CONNECTION_WILL_CONNECT,
  230. connection
  231. };
  232. }
  233. /**
  234. * Constructs options to be passed to the constructor of {@code JitsiConnection}
  235. * based on the redux state.
  236. *
  237. * @param {Object} state - The redux state.
  238. * @returns {Object} The options to be passed to the constructor of
  239. * {@code JitsiConnection}.
  240. */
  241. function _constructOptions(state) {
  242. const defaultOptions = state['features/base/connection'].options;
  243. const options = _.merge(
  244. {},
  245. defaultOptions,
  246. // Lib-jitsi-meet wants the config passed in multiple places and here is
  247. // the latest one I have discovered.
  248. state['features/base/config'],
  249. );
  250. let { bosh } = options;
  251. if (bosh) {
  252. // Append room to the URL's search.
  253. const { room } = state['features/base/conference'];
  254. // XXX The Jitsi Meet deployments require the room argument to be in
  255. // lower case at the time of this writing but, unfortunately, they do
  256. // not ignore case themselves.
  257. room && (bosh += `?room=${room.toLowerCase()}`);
  258. // XXX By default, config.js does not add a protocol to the BOSH URL.
  259. // Which trips React Native. Make sure there is a protocol in order to
  260. // satisfy React Native.
  261. if (bosh !== defaultOptions.bosh
  262. && !parseStandardURIString(bosh).protocol) {
  263. const { protocol } = parseStandardURIString(defaultOptions.bosh);
  264. protocol && (bosh = protocol + bosh);
  265. }
  266. options.bosh = bosh;
  267. }
  268. return options;
  269. }
  270. /**
  271. * Closes connection.
  272. *
  273. * @returns {Function}
  274. */
  275. export function disconnect() {
  276. return (dispatch: Dispatch<*>, getState: Function): Promise<void> => {
  277. const state = getState();
  278. // The conference we have already joined or are joining.
  279. const conference_ = getCurrentConference(state);
  280. // Promise which completes when the conference has been left and the
  281. // connection has been disconnected.
  282. let promise;
  283. // Leave the conference.
  284. if (conference_) {
  285. // In a fashion similar to JitsiConference's CONFERENCE_LEFT event
  286. // (and the respective Redux action) which is fired after the
  287. // conference has been left, notify the application about the
  288. // intention to leave the conference.
  289. dispatch(conferenceWillLeave(conference_));
  290. promise
  291. = conference_.leave()
  292. .catch(error => {
  293. logger.warn(
  294. 'JitsiConference.leave() rejected with:',
  295. error);
  296. // The library lib-jitsi-meet failed to make the
  297. // JitsiConference leave. Which may be because
  298. // JitsiConference thinks it has already left.
  299. // Regardless of the failure reason, continue in
  300. // jitsi-meet as if the leave has succeeded.
  301. dispatch(conferenceLeft(conference_));
  302. });
  303. } else {
  304. promise = Promise.resolve();
  305. }
  306. // Disconnect the connection.
  307. const { connecting, connection } = state['features/base/connection'];
  308. // The connection we have already connected or are connecting.
  309. const connection_ = connection || connecting;
  310. if (connection_) {
  311. promise = promise.then(() => connection_.disconnect());
  312. }
  313. return promise;
  314. };
  315. }
  316. /**
  317. * Sets the location URL of the application, connecton, conference, etc.
  318. *
  319. * @param {URL} [locationURL] - The location URL of the application,
  320. * connection, conference, etc.
  321. * @returns {{
  322. * type: SET_LOCATION_URL,
  323. * locationURL: URL
  324. * }}
  325. */
  326. export function setLocationURL(locationURL: ?URL) {
  327. return {
  328. type: SET_LOCATION_URL,
  329. locationURL
  330. };
  331. }