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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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_DOMAIN
  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. * Closes connection.
  100. *
  101. * @returns {Function}
  102. */
  103. export function disconnect() {
  104. return (dispatch: Dispatch<*>, getState: Function) => {
  105. const state = getState();
  106. const conference = state['features/base/conference'].conference;
  107. const connection = state['features/base/connection'].connection;
  108. let promise;
  109. // Leave the conference.
  110. if (conference) {
  111. // In a fashion similar to JitsiConference's CONFERENCE_LEFT event
  112. // (and the respective Redux action) which is fired after the
  113. // conference has been left, notify the application about the
  114. // intention to leave the conference.
  115. dispatch(conferenceWillLeave(conference));
  116. promise = conference.leave();
  117. } else {
  118. promise = Promise.resolve();
  119. }
  120. // Disconnect the connection.
  121. if (connection) {
  122. promise = promise.then(() => connection.disconnect());
  123. }
  124. return promise;
  125. };
  126. }
  127. /**
  128. * Sets connection domain.
  129. *
  130. * @param {string} domain - Domain name.
  131. * @returns {{
  132. * type: SET_DOMAIN,
  133. * domain: string
  134. * }}
  135. */
  136. export function setDomain(domain: string) {
  137. return {
  138. type: SET_DOMAIN,
  139. domain
  140. };
  141. }
  142. /**
  143. * Create an action for when the signaling connection has been lost.
  144. *
  145. * @param {JitsiConnection} connection - The JitsiConnection which disconnected.
  146. * @param {string} message - Error message.
  147. * @private
  148. * @returns {{
  149. * type: CONNECTION_DISCONNECTED,
  150. * connection: JitsiConnection,
  151. * message: string
  152. * }}
  153. */
  154. function _connectionDisconnected(connection, message: string) {
  155. return {
  156. type: CONNECTION_DISCONNECTED,
  157. connection,
  158. message
  159. };
  160. }
  161. /**
  162. * Create an action for when the signaling connection has been established.
  163. *
  164. * @param {JitsiConnection} connection - The JitsiConnection which was
  165. * established.
  166. * @returns {{
  167. * type: CONNECTION_ESTABLISHED,
  168. * connection: JitsiConnection
  169. * }}
  170. * @public
  171. */
  172. export function connectionEstablished(connection: Object) {
  173. return {
  174. type: CONNECTION_ESTABLISHED,
  175. connection
  176. };
  177. }
  178. /**
  179. * Create an action for when the signaling connection could not be created.
  180. *
  181. * @param {JitsiConnection} connection - The JitsiConnection which failed.
  182. * @param {string} error - Error.
  183. * @param {string} errorMessage - Error message.
  184. * @returns {{
  185. * type: CONNECTION_FAILED,
  186. * connection: JitsiConnection,
  187. * error: string,
  188. * errorMessage: string
  189. * }}
  190. * @public
  191. */
  192. export function connectionFailed(
  193. connection: Object, error: string, errorMessage: string) {
  194. return {
  195. type: CONNECTION_FAILED,
  196. connection,
  197. error,
  198. errorMessage
  199. };
  200. }