Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

actions.native.js 6.7KB

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