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

actions.native.js 7.5KB

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