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

actions.native.js 6.0KB

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