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

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