You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

actions.native.js 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // @flow
  2. import _ from 'lodash';
  3. import type { Dispatch } from 'redux';
  4. import { 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. /**
  15. * Opens new connection.
  16. *
  17. * @param {string} [id] - The XMPP user's ID (e.g. user@server.com).
  18. * @param {string} [password] - The XMPP user's password.
  19. * @returns {Function}
  20. */
  21. export function connect(id: ?string, password: ?string) {
  22. return (dispatch: Dispatch<*>, getState: Function) => {
  23. const state = getState();
  24. const options = _constructOptions(state);
  25. const { issuer, jwt } = state['features/base/jwt'];
  26. const connection
  27. = new JitsiMeetJS.JitsiConnection(
  28. options.appId,
  29. jwt && issuer && issuer !== 'anonymous' ? jwt : undefined,
  30. options);
  31. dispatch(_connectionWillConnect(connection));
  32. connection.addEventListener(
  33. JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  34. _onConnectionDisconnected);
  35. connection.addEventListener(
  36. JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  37. _onConnectionEstablished);
  38. connection.addEventListener(
  39. JitsiConnectionEvents.CONNECTION_FAILED,
  40. _onConnectionFailed);
  41. return connection.connect({
  42. id,
  43. password
  44. });
  45. /**
  46. * Dispatches CONNECTION_DISCONNECTED action when connection is
  47. * disconnected.
  48. *
  49. * @param {string} message - Disconnect reason.
  50. * @private
  51. * @returns {void}
  52. */
  53. function _onConnectionDisconnected(message: string) {
  54. connection.removeEventListener(
  55. JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  56. _onConnectionDisconnected);
  57. dispatch(_connectionDisconnected(connection, message));
  58. }
  59. /**
  60. * Resolves external promise when connection is established.
  61. *
  62. * @private
  63. * @returns {void}
  64. */
  65. function _onConnectionEstablished() {
  66. unsubscribe();
  67. dispatch(connectionEstablished(connection));
  68. }
  69. /**
  70. * Rejects external promise when connection fails.
  71. *
  72. * @param {JitsiConnectionErrors} err - Connection error.
  73. * @param {string} [msg] - Error message supplied by lib-jitsi-meet.
  74. * @param {Object} [credentials] - The invalid credentials that were
  75. * used to authenticate and the authentication failed.
  76. * @param {string} [credentials.jid] - The XMPP user's ID.
  77. * @param {string} [credentials.password] - The XMPP user's password.
  78. * @private
  79. * @returns {void}
  80. */
  81. function _onConnectionFailed(err, msg, credentials) {
  82. unsubscribe();
  83. console.error('CONNECTION FAILED:', err, msg);
  84. dispatch(connectionFailed(connection, err, msg, credentials));
  85. }
  86. /**
  87. * Unsubscribes connection instance from CONNECTION_ESTABLISHED
  88. * and CONNECTION_FAILED events.
  89. *
  90. * @returns {void}
  91. */
  92. function unsubscribe() {
  93. connection.removeEventListener(
  94. JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  95. _onConnectionEstablished);
  96. connection.removeEventListener(
  97. JitsiConnectionEvents.CONNECTION_FAILED,
  98. _onConnectionFailed);
  99. }
  100. };
  101. }
  102. /**
  103. * Create an action for when the signaling connection has been lost.
  104. *
  105. * @param {JitsiConnection} connection - The JitsiConnection which disconnected.
  106. * @param {string} message - Error message.
  107. * @private
  108. * @returns {{
  109. * type: CONNECTION_DISCONNECTED,
  110. * connection: JitsiConnection,
  111. * message: string
  112. * }}
  113. */
  114. function _connectionDisconnected(connection: Object, message: string) {
  115. return {
  116. type: CONNECTION_DISCONNECTED,
  117. connection,
  118. message
  119. };
  120. }
  121. /**
  122. * Create an action for when a connection will connect.
  123. *
  124. * @param {JitsiConnection} connection - The JitsiConnection which will connect.
  125. * @private
  126. * @returns {{
  127. * type: CONNECTION_WILL_CONNECT,
  128. * connection: JitsiConnection
  129. * }}
  130. */
  131. function _connectionWillConnect(connection) {
  132. return {
  133. type: CONNECTION_WILL_CONNECT,
  134. connection
  135. };
  136. }
  137. /**
  138. * Create an action for when the signaling connection has been established.
  139. *
  140. * @param {JitsiConnection} connection - The JitsiConnection which was
  141. * established.
  142. * @public
  143. * @returns {{
  144. * type: CONNECTION_ESTABLISHED,
  145. * connection: JitsiConnection
  146. * }}
  147. */
  148. export function connectionEstablished(connection: Object) {
  149. return {
  150. type: CONNECTION_ESTABLISHED,
  151. connection
  152. };
  153. }
  154. /* eslint-disable max-params */
  155. /**
  156. * Create an action for when the signaling connection could not be created.
  157. *
  158. * @param {JitsiConnection} connection - The JitsiConnection which failed.
  159. * @param {string} error - Error.
  160. * @param {string} [message] - Error message.
  161. * @param {Object} [credentials] - The invalid credentials that failed
  162. * the authentication.
  163. * @public
  164. * @returns {{
  165. * type: CONNECTION_FAILED,
  166. * connection: JitsiConnection,
  167. * error: Object
  168. * }}
  169. */
  170. export function connectionFailed(
  171. connection: Object,
  172. error: string,
  173. message: ?string,
  174. credentials: ?Object) {
  175. return {
  176. type: CONNECTION_FAILED,
  177. connection,
  178. // Make the error resemble an Error instance (to the extent that
  179. // jitsi-meet needs it).
  180. error: {
  181. credentials:
  182. credentials && Object.keys(credentials).length
  183. ? credentials
  184. : undefined,
  185. message,
  186. name: error
  187. }
  188. };
  189. }
  190. /* eslint-enable max-params */
  191. /**
  192. * Constructs options to be passed to the constructor of {@code JitsiConnection}
  193. * based on the redux state.
  194. *
  195. * @param {Object} state - The redux state.
  196. * @returns {Object} The options to be passed to the constructor of
  197. * {@code JitsiConnection}.
  198. */
  199. function _constructOptions(state) {
  200. const defaultOptions = state['features/base/connection'].options;
  201. const options = _.merge(
  202. {},
  203. defaultOptions,
  204. // Lib-jitsi-meet wants the config passed in multiple places and here is
  205. // the latest one I have discovered.
  206. state['features/base/config'],
  207. );
  208. let { bosh } = options;
  209. if (bosh) {
  210. // Append room to the URL's search.
  211. const { room } = state['features/base/conference'];
  212. // XXX The Jitsi Meet deployments require the room argument to be in
  213. // lower case at the time of this writing but, unfortunately, they do
  214. // not ignore case themselves.
  215. room && (bosh += `?room=${room.toLowerCase()}`);
  216. // XXX By default, config.js does not add a protocol to the BOSH URL.
  217. // Which trips React Native. Make sure there is a protocol in order to
  218. // satisfy React Native.
  219. if (bosh !== defaultOptions.bosh
  220. && !parseStandardURIString(bosh).protocol) {
  221. const { protocol } = parseStandardURIString(defaultOptions.bosh);
  222. protocol && (bosh = protocol + bosh);
  223. }
  224. options.bosh = bosh;
  225. }
  226. return options;
  227. }
  228. /**
  229. * Closes connection.
  230. *
  231. * @returns {Function}
  232. */
  233. export function disconnect() {
  234. return (dispatch: Dispatch<*>, getState: Function): Promise<void> => {
  235. const state = getState();
  236. const { conference, joining } = state['features/base/conference'];
  237. // The conference we have already joined or are joining.
  238. const conference_ = conference || joining;
  239. // Promise which completes when the conference has been left and the
  240. // connection has been disconnected.
  241. let promise;
  242. // Leave the conference.
  243. if (conference_) {
  244. // In a fashion similar to JitsiConference's CONFERENCE_LEFT event
  245. // (and the respective Redux action) which is fired after the
  246. // conference has been left, notify the application about the
  247. // intention to leave the conference.
  248. dispatch(conferenceWillLeave(conference_));
  249. promise = conference_.leave();
  250. } else {
  251. promise = Promise.resolve();
  252. }
  253. // Disconnect the connection.
  254. const { connecting, connection } = state['features/base/connection'];
  255. // The connection we have already connected or are connecting.
  256. const connection_ = connection || connecting;
  257. if (connection_) {
  258. promise = promise.then(() => connection_.disconnect());
  259. }
  260. return promise;
  261. };
  262. }
  263. /**
  264. * Sets the location URL of the application, connecton, conference, etc.
  265. *
  266. * @param {URL} [locationURL] - The location URL of the application,
  267. * connection, conference, etc.
  268. * @returns {{
  269. * type: SET_LOCATION_URL,
  270. * locationURL: URL
  271. * }}
  272. */
  273. export function setLocationURL(locationURL: ?URL) {
  274. return {
  275. type: SET_LOCATION_URL,
  276. locationURL
  277. };
  278. }