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

actions.js 5.6KB

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