Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

actions.native.js 11KB

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