您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

actions.js 5.8KB

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