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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. import './reducer';
  12. const JitsiConnectionEvents = JitsiMeetJS.events.connection;
  13. /**
  14. * Opens new connection.
  15. *
  16. * @returns {Function}
  17. */
  18. export function connect() {
  19. return (dispatch: Dispatch<*>, getState: Function) => {
  20. const state = getState();
  21. const connectionOptions
  22. = state['features/base/connection'].connectionOptions;
  23. const room = state['features/base/conference'].room;
  24. const connection
  25. = new JitsiMeetJS.JitsiConnection(
  26. connectionOptions.appId,
  27. connectionOptions.token,
  28. {
  29. ...connectionOptions,
  30. bosh:
  31. connectionOptions.bosh
  32. // XXX The Jitsi Meet deployments require the room
  33. // argument to be in lower case at the time of this
  34. // writing but, unfortunately, they do not ignore
  35. // case themselves.
  36. + (room ? `?room=${room.toLowerCase()}` : '')
  37. });
  38. connection.addEventListener(
  39. JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  40. connectionDisconnected);
  41. connection.addEventListener(
  42. JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  43. connectionEstablished);
  44. connection.addEventListener(
  45. JitsiConnectionEvents.CONNECTION_FAILED,
  46. connectionFailed);
  47. connection.connect();
  48. /**
  49. * Dispatches CONNECTION_DISCONNECTED action when connection is
  50. * disconnected.
  51. *
  52. * @param {string} message - Disconnect reason.
  53. * @returns {void}
  54. */
  55. function connectionDisconnected(message: string) {
  56. connection.removeEventListener(
  57. JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  58. connectionDisconnected);
  59. dispatch(_connectionDisconnected(connection, message));
  60. }
  61. /**
  62. * Resolves external promise when connection is established.
  63. *
  64. * @returns {void}
  65. */
  66. function connectionEstablished() {
  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. */
  76. function connectionFailed(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. connectionEstablished);
  91. connection.removeEventListener(
  92. JitsiConnectionEvents.CONNECTION_FAILED,
  93. connectionFailed);
  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. * @private
  166. * @returns {{
  167. * type: CONNECTION_ESTABLISHED,
  168. * connection: JitsiConnection
  169. * }}
  170. */
  171. function _connectionEstablished(connection) {
  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 message.
  182. * @private
  183. * @returns {{
  184. * type: CONNECTION_FAILED,
  185. * connection: JitsiConnection,
  186. * error: string
  187. * }}
  188. */
  189. function _connectionFailed(connection, error: string) {
  190. return {
  191. type: CONNECTION_FAILED,
  192. connection,
  193. error
  194. };
  195. }