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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. const { options } = state['features/base/connection'];
  20. const { issuer, jwt } = state['features/jwt'];
  21. const { room } = state['features/base/conference'];
  22. const connection
  23. = new JitsiMeetJS.JitsiConnection(
  24. options.appId,
  25. jwt && issuer && issuer !== 'anonymous' ? jwt : undefined,
  26. {
  27. ...options,
  28. bosh:
  29. options.bosh
  30. // XXX The Jitsi Meet deployments require the room
  31. // argument to be in lower case at the time of this
  32. // writing but, unfortunately, they do not ignore
  33. // case themselves.
  34. + (room ? `?room=${room.toLowerCase()}` : '')
  35. });
  36. connection.addEventListener(
  37. JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  38. _onConnectionDisconnected);
  39. connection.addEventListener(
  40. JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  41. _onConnectionEstablished);
  42. connection.addEventListener(
  43. JitsiConnectionEvents.CONNECTION_FAILED,
  44. _onConnectionFailed);
  45. connection.connect();
  46. /**
  47. * Dispatches CONNECTION_DISCONNECTED action when connection is
  48. * disconnected.
  49. *
  50. * @param {string} message - Disconnect reason.
  51. * @returns {void}
  52. * @private
  53. */
  54. function _onConnectionDisconnected(message: string) {
  55. connection.removeEventListener(
  56. JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  57. _onConnectionDisconnected);
  58. dispatch(_connectionDisconnected(connection, message));
  59. }
  60. /**
  61. * Resolves external promise when connection is established.
  62. *
  63. * @returns {void}
  64. * @private
  65. */
  66. function _onConnectionEstablished() {
  67. unsubscribe();
  68. dispatch(connectionEstablished(connection));
  69. }
  70. /**
  71. * Rejects external promise when connection fails.
  72. *
  73. * @param {JitsiConnectionErrors} err - Connection error.
  74. * @returns {void}
  75. * @private
  76. */
  77. function _onConnectionFailed(err) {
  78. unsubscribe();
  79. console.error('CONNECTION FAILED:', err);
  80. dispatch(connectionFailed(connection, err));
  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 the signaling connection has been established.
  119. *
  120. * @param {JitsiConnection} connection - The JitsiConnection which was
  121. * established.
  122. * @public
  123. * @returns {{
  124. * type: CONNECTION_ESTABLISHED,
  125. * connection: JitsiConnection
  126. * }}
  127. */
  128. export function connectionEstablished(connection: Object) {
  129. return {
  130. type: CONNECTION_ESTABLISHED,
  131. connection
  132. };
  133. }
  134. /**
  135. * Create an action for when the signaling connection could not be created.
  136. *
  137. * @param {JitsiConnection} connection - The JitsiConnection which failed.
  138. * @param {string} error - Error.
  139. * @param {string} message - Error message.
  140. * @public
  141. * @returns {{
  142. * type: CONNECTION_FAILED,
  143. * connection: JitsiConnection,
  144. * error: string,
  145. * message: string
  146. * }}
  147. */
  148. export function connectionFailed(
  149. connection: Object,
  150. error: string,
  151. message: ?string) {
  152. return {
  153. type: CONNECTION_FAILED,
  154. connection,
  155. error,
  156. message
  157. };
  158. }
  159. /**
  160. * Closes connection.
  161. *
  162. * @returns {Function}
  163. */
  164. export function disconnect() {
  165. return (dispatch: Dispatch<*>, getState: Function) => {
  166. const state = getState();
  167. const { conference } = state['features/base/conference'];
  168. const { connection } = state['features/base/connection'];
  169. let promise;
  170. // Leave the conference.
  171. if (conference) {
  172. // In a fashion similar to JitsiConference's CONFERENCE_LEFT event
  173. // (and the respective Redux action) which is fired after the
  174. // conference has been left, notify the application about the
  175. // intention to leave the conference.
  176. dispatch(conferenceWillLeave(conference));
  177. promise = conference.leave();
  178. } else {
  179. promise = Promise.resolve();
  180. }
  181. // Disconnect the connection.
  182. if (connection) {
  183. promise = promise.then(() => connection.disconnect());
  184. }
  185. return promise;
  186. };
  187. }
  188. /**
  189. * Sets the location URL of the application, connecton, conference, etc.
  190. *
  191. * @param {URL} [locationURL] - The location URL of the application,
  192. * connection, conference, etc.
  193. * @returns {{
  194. * type: SET_LOCATION_URL,
  195. * locationURL: URL
  196. * }}
  197. */
  198. export function setLocationURL(locationURL: ?URL) {
  199. return {
  200. type: SET_LOCATION_URL,
  201. locationURL
  202. };
  203. }