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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import { conferenceWillLeave } from '../conference';
  2. import JitsiMeetJS from '../lib-jitsi-meet';
  3. import {
  4. CONNECTION_DISCONNECTED,
  5. CONNECTION_ESTABLISHED,
  6. CONNECTION_FAILED,
  7. SET_DOMAIN
  8. } from './actionTypes';
  9. import './reducer';
  10. const JitsiConnectionEvents = JitsiMeetJS.events.connection;
  11. /**
  12. * Opens new connection.
  13. *
  14. * @returns {Promise<JitsiConnection>}
  15. */
  16. export function connect() {
  17. return (dispatch, getState) => {
  18. const state = getState();
  19. const connectionOptions
  20. = state['features/base/connection'].connectionOptions;
  21. const room = state['features/base/conference'].room;
  22. const connection
  23. = new JitsiMeetJS.JitsiConnection(
  24. connectionOptions.appId,
  25. connectionOptions.token,
  26. {
  27. ...connectionOptions,
  28. bosh:
  29. connectionOptions.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. connectionDisconnected);
  39. connection.addEventListener(
  40. JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  41. connectionEstablished);
  42. connection.addEventListener(
  43. JitsiConnectionEvents.CONNECTION_FAILED,
  44. connectionFailed);
  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. */
  53. function connectionDisconnected(message) {
  54. connection.removeEventListener(
  55. JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  56. connectionDisconnected);
  57. dispatch(_connectionDisconnected(connection, message));
  58. }
  59. /**
  60. * Resolves external promise when connection is established.
  61. *
  62. * @returns {void}
  63. */
  64. function connectionEstablished() {
  65. unsubscribe();
  66. dispatch(_connectionEstablished(connection));
  67. }
  68. /**
  69. * Rejects external promise when connection fails.
  70. *
  71. * @param {JitsiConnectionErrors} err - Connection error.
  72. * @returns {void}
  73. */
  74. function connectionFailed(err) {
  75. unsubscribe();
  76. console.error('CONNECTION FAILED:', err);
  77. dispatch(_connectionFailed(connection, err));
  78. }
  79. /**
  80. * Unsubscribes connection instance from CONNECTION_ESTABLISHED
  81. * and CONNECTION_FAILED events.
  82. *
  83. * @returns {void}
  84. */
  85. function unsubscribe() {
  86. connection.removeEventListener(
  87. JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  88. connectionEstablished);
  89. connection.removeEventListener(
  90. JitsiConnectionEvents.CONNECTION_FAILED,
  91. connectionFailed);
  92. }
  93. };
  94. }
  95. /**
  96. * Closes connection.
  97. *
  98. * @returns {Function}
  99. */
  100. export function disconnect() {
  101. return (dispatch, getState) => {
  102. const state = getState();
  103. const conference = state['features/base/conference'].conference;
  104. const connection = state['features/base/connection'].connection;
  105. let promise;
  106. // Leave the conference.
  107. if (conference) {
  108. // In a fashion similar to JitsiConference's CONFERENCE_LEFT event
  109. // (and the respective Redux action) which is fired after the
  110. // conference has been left, notify the application about the
  111. // intention to leave the conference.
  112. dispatch(conferenceWillLeave(conference));
  113. promise = conference.leave();
  114. } else {
  115. promise = Promise.resolve();
  116. }
  117. // Disconnect the connection.
  118. if (connection) {
  119. promise = promise.then(() => connection.disconnect());
  120. }
  121. return promise;
  122. };
  123. }
  124. /**
  125. * Sets connection domain.
  126. *
  127. * @param {string} domain - Domain name.
  128. * @returns {{
  129. * type: SET_DOMAIN,
  130. * domain: string
  131. * }}
  132. */
  133. export function setDomain(domain) {
  134. return {
  135. type: SET_DOMAIN,
  136. domain
  137. };
  138. }
  139. /**
  140. * Create an action for when the signaling connection has been lost.
  141. *
  142. * @param {JitsiConnection} connection - The JitsiConnection which disconnected.
  143. * @param {string} message - Error message.
  144. * @private
  145. * @returns {{
  146. * type: CONNECTION_DISCONNECTED,
  147. * connection: JitsiConnection,
  148. * message: string
  149. * }}
  150. */
  151. function _connectionDisconnected(connection, message) {
  152. return {
  153. type: CONNECTION_DISCONNECTED,
  154. connection,
  155. message
  156. };
  157. }
  158. /**
  159. * Create an action for when the signaling connection has been established.
  160. *
  161. * @param {JitsiConnection} connection - The JitsiConnection which was
  162. * established.
  163. * @private
  164. * @returns {{
  165. * type: CONNECTION_ESTABLISHED,
  166. * connection: JitsiConnection
  167. * }}
  168. */
  169. function _connectionEstablished(connection) {
  170. return {
  171. type: CONNECTION_ESTABLISHED,
  172. connection
  173. };
  174. }
  175. /**
  176. * Create an action for when the signaling connection could not be created.
  177. *
  178. * @param {JitsiConnection} connection - The JitsiConnection which failed.
  179. * @param {string} error - Error message.
  180. * @private
  181. * @returns {{
  182. * type: CONNECTION_FAILED,
  183. * connection: JitsiConnection,
  184. * error: string
  185. * }}
  186. */
  187. function _connectionFailed(connection, error) {
  188. return {
  189. type: CONNECTION_FAILED,
  190. connection,
  191. error
  192. };
  193. }