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

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