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

actions.native.js 11KB

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