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

actions.native.js 8.2KB

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