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 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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/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. 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. * @returns {void}
  51. * @private
  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. * @returns {void}
  63. * @private
  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. * @returns {void}
  75. * @private
  76. */
  77. function _onConnectionFailed(err, msg) {
  78. unsubscribe();
  79. console.error('CONNECTION FAILED:', err, msg);
  80. dispatch(connectionFailed(connection, err, msg));
  81. }
  82. /**
  83. * Unsubscribes connection instance from CONNECTION_ESTABLISHED
  84. * and CONNECTION_FAILED events.
  85. *
  86. * @returns {void}
  87. */
  88. function unsubscribe() {
  89. connection.removeEventListener(
  90. JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  91. _onConnectionEstablished);
  92. connection.removeEventListener(
  93. JitsiConnectionEvents.CONNECTION_FAILED,
  94. _onConnectionFailed);
  95. }
  96. };
  97. }
  98. /**
  99. * Create an action for when the signaling connection has been lost.
  100. *
  101. * @param {JitsiConnection} connection - The JitsiConnection which disconnected.
  102. * @param {string} message - Error message.
  103. * @private
  104. * @returns {{
  105. * type: CONNECTION_DISCONNECTED,
  106. * connection: JitsiConnection,
  107. * message: string
  108. * }}
  109. */
  110. function _connectionDisconnected(connection: Object, message: string) {
  111. return {
  112. type: CONNECTION_DISCONNECTED,
  113. connection,
  114. message
  115. };
  116. }
  117. /**
  118. * Create an action for when a connection will connect.
  119. *
  120. * @param {JitsiConnection} connection - The JitsiConnection which will connect.
  121. * @private
  122. * @returns {{
  123. * type: CONNECTION_WILL_CONNECT,
  124. * connection: JitsiConnection
  125. * }}
  126. */
  127. function _connectionWillConnect(connection) {
  128. return {
  129. type: CONNECTION_WILL_CONNECT,
  130. connection
  131. };
  132. }
  133. /**
  134. * Create an action for when the signaling connection has been established.
  135. *
  136. * @param {JitsiConnection} connection - The JitsiConnection which was
  137. * established.
  138. * @public
  139. * @returns {{
  140. * type: CONNECTION_ESTABLISHED,
  141. * connection: JitsiConnection
  142. * }}
  143. */
  144. export function connectionEstablished(connection: Object) {
  145. return {
  146. type: CONNECTION_ESTABLISHED,
  147. connection
  148. };
  149. }
  150. /**
  151. * Create an action for when the signaling connection could not be created.
  152. *
  153. * @param {JitsiConnection} connection - The JitsiConnection which failed.
  154. * @param {string} error - Error.
  155. * @param {string} message - Error message.
  156. * @public
  157. * @returns {{
  158. * type: CONNECTION_FAILED,
  159. * connection: JitsiConnection,
  160. * error: string,
  161. * message: string
  162. * }}
  163. */
  164. export function connectionFailed(
  165. connection: Object,
  166. error: string,
  167. message: ?string) {
  168. return {
  169. type: CONNECTION_FAILED,
  170. connection,
  171. error,
  172. message
  173. };
  174. }
  175. /**
  176. * Constructs options to be passed to the constructor of {@code JitsiConnection}
  177. * based on the redux state.
  178. *
  179. * @param {Object} state - The redux state.
  180. * @returns {Object} The options to be passed to the constructor of
  181. * {@code JitsiConnection}.
  182. */
  183. function _constructOptions(state) {
  184. const defaultOptions = state['features/base/connection'].options;
  185. const options = _.merge(
  186. {},
  187. defaultOptions,
  188. // Lib-jitsi-meet wants the config passed in multiple places and here is
  189. // the latest one I have discovered.
  190. state['features/base/config'],
  191. );
  192. let { bosh } = options;
  193. if (bosh) {
  194. // Append room to the URL's search.
  195. const { room } = state['features/base/conference'];
  196. // XXX The Jitsi Meet deployments require the room argument to be in
  197. // lower case at the time of this writing but, unfortunately, they do
  198. // not ignore case themselves.
  199. room && (bosh += `?room=${room.toLowerCase()}`);
  200. // XXX By default, config.js does not add a protocol to the BOSH URL.
  201. // Which trips React Native. Make sure there is a protocol in order to
  202. // satisfy React Native.
  203. if (bosh !== defaultOptions.bosh
  204. && !parseStandardURIString(bosh).protocol) {
  205. const { protocol } = parseStandardURIString(defaultOptions.bosh);
  206. protocol && (bosh = protocol + bosh);
  207. }
  208. options.bosh = bosh;
  209. }
  210. return options;
  211. }
  212. /**
  213. * Closes connection.
  214. *
  215. * @returns {Function}
  216. */
  217. export function disconnect() {
  218. return (dispatch: Dispatch<*>, getState: Function) => {
  219. const state = getState();
  220. const { conference, joining } = state['features/base/conference'];
  221. // The conference we are joining or have already joined.
  222. const conference_ = conference || joining;
  223. // Promise which completes when the conference has been left and the
  224. // connection has been disconnected.
  225. let promise;
  226. // Leave the conference.
  227. if (conference_) {
  228. // In a fashion similar to JitsiConference's CONFERENCE_LEFT event
  229. // (and the respective Redux action) which is fired after the
  230. // conference has been left, notify the application about the
  231. // intention to leave the conference.
  232. dispatch(conferenceWillLeave(conference_));
  233. promise = conference_.leave();
  234. } else {
  235. promise = Promise.resolve();
  236. }
  237. // Disconnect the connection.
  238. const { connecting, connection } = state['features/base/connection'];
  239. // The connection we are connecting or have already connected.
  240. const connection_ = connection || connecting;
  241. if (connection_) {
  242. promise = promise.then(() => connection_.disconnect());
  243. }
  244. return promise;
  245. };
  246. }
  247. /**
  248. * Sets the location URL of the application, connecton, conference, etc.
  249. *
  250. * @param {URL} [locationURL] - The location URL of the application,
  251. * connection, conference, etc.
  252. * @returns {{
  253. * type: SET_LOCATION_URL,
  254. * locationURL: URL
  255. * }}
  256. */
  257. export function setLocationURL(locationURL: ?URL) {
  258. return {
  259. type: SET_LOCATION_URL,
  260. locationURL
  261. };
  262. }