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

JitsiConnection.js 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import { getLogger } from '@jitsi/logger';
  2. import JitsiConference from './JitsiConference';
  3. import * as JitsiConnectionEvents from './JitsiConnectionEvents';
  4. import RTCStats from './modules/RTCStats/RTCStats';
  5. import FeatureFlags from './modules/flags/FeatureFlags';
  6. import Statistics from './modules/statistics/statistics';
  7. import XMPP from './modules/xmpp/xmpp';
  8. import {
  9. CONNECTION_DISCONNECTED as ANALYTICS_CONNECTION_DISCONNECTED,
  10. createConnectionFailedEvent
  11. } from './service/statistics/AnalyticsEvents';
  12. const logger = getLogger('JitsiConnection');
  13. /**
  14. * Creates a new connection object for the Jitsi Meet server side video
  15. * conferencing service. Provides access to the JitsiConference interface.
  16. * @param appID identification for the provider of Jitsi Meet video conferencing
  17. * services.
  18. * @param token the JWT token used to authenticate with the server(optional)
  19. * @param options Object with properties / settings related to connection with
  20. * the server.
  21. * @constructor
  22. */
  23. export default function JitsiConnection(appID, token, options) {
  24. this.appID = appID;
  25. this.token = token;
  26. this.options = options;
  27. // Initialize the feature flags so that they are advertised through the disco-info.
  28. FeatureFlags.init(options.flags || {});
  29. this.xmpp = new XMPP(options, token);
  30. /* eslint-disable max-params */
  31. this.addEventListener(JitsiConnectionEvents.CONNECTION_FAILED,
  32. (errType, msg, credentials, details) => {
  33. Statistics.sendAnalyticsAndLog(
  34. createConnectionFailedEvent(errType, msg, details));
  35. });
  36. /* eslint-enable max-params */
  37. this.addEventListener(JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  38. msg => {
  39. // we can see disconnects from normal tab closing of the browser
  40. // and then there are no msgs, but we want to log only disconnects
  41. // when there is real error
  42. // XXX Do we need the difference in handling between the log and
  43. // analytics event here?
  44. if (msg) {
  45. Statistics.sendAnalytics(
  46. ANALYTICS_CONNECTION_DISCONNECTED,
  47. { message: msg });
  48. }
  49. });
  50. }
  51. /**
  52. * Connect the client with the server.
  53. * @param options {object} connecting options (for example authentications parameters).
  54. * @param options.id {string} The username to use when connecting, if any.
  55. * @param options.password {string} The password to use when connecting with username, if any.
  56. * @param options.name {string} The name of the room/conference we will be connecting to. This is needed on connection
  57. * time to be able to send conference-request over http. If missing the flow where we send conference-iq to jicofo over
  58. * the established xmpp connection will be used, even in the case where we have configured conference http request url
  59. * to be used.
  60. */
  61. JitsiConnection.prototype.connect = function(options = {}) {
  62. RTCStats.startWithConnection(this);
  63. // if we get redirected, we set disableFocus to skip sending the conference request twice
  64. if (this.xmpp.moderator.targetUrl && !this.options.disableFocus && options.name) {
  65. this.xmpp.moderator.sendConferenceRequest(this.xmpp.getRoomJid(options.name))
  66. .then(() => {
  67. this.xmpp.connect(options.id, options.password);
  68. })
  69. .catch(e => logger.trace('sendConferenceRequest rejected', e));
  70. } else {
  71. this.xmpp.connect(options.id, options.password);
  72. }
  73. };
  74. /**
  75. * Attach to existing connection. Can be used for optimizations. For example:
  76. * if the connection is created on the server we can attach to it and start
  77. * using it.
  78. *
  79. * @param options {object} connecting options - rid, sid and jid.
  80. */
  81. JitsiConnection.prototype.attach = function(options) {
  82. this.xmpp.attach(options);
  83. };
  84. /**
  85. * Disconnect the client from the server.
  86. * @returns {Promise} - Resolves when the disconnect process is finished or rejects with an error.
  87. */
  88. JitsiConnection.prototype.disconnect = function(...args) {
  89. // XXX Forward any arguments passed to JitsiConnection.disconnect to
  90. // XMPP.disconnect. For example, the caller of JitsiConnection.disconnect
  91. // may optionally pass the event which triggered the disconnect in order to
  92. // provide the implementation with finer-grained context.
  93. return this.xmpp.disconnect(...args);
  94. };
  95. /**
  96. * Returns the jid of the participant associated with the XMPP connection.
  97. *
  98. * @returns {string} The jid of the participant.
  99. */
  100. JitsiConnection.prototype.getJid = function() {
  101. return this.xmpp.getJid();
  102. };
  103. /**
  104. * This method allows renewal of the tokens if they are expiring.
  105. * @param token the new token.
  106. */
  107. JitsiConnection.prototype.setToken = function(token) {
  108. this.token = token;
  109. };
  110. /**
  111. * Creates and joins new conference.
  112. * @param name the name of the conference; if null - a generated name will be
  113. * provided from the api
  114. * @param options Object with properties / settings related to the conference
  115. * that will be created.
  116. * @returns {JitsiConference} returns the new conference object.
  117. */
  118. JitsiConnection.prototype.initJitsiConference = function(name, options) {
  119. return new JitsiConference({
  120. name,
  121. config: options,
  122. connection: this
  123. });
  124. };
  125. /**
  126. * Subscribes the passed listener to the event.
  127. * @param event {JitsiConnectionEvents} the connection event.
  128. * @param listener {Function} the function that will receive the event
  129. */
  130. JitsiConnection.prototype.addEventListener = function(event, listener) {
  131. this.xmpp.addListener(event, listener);
  132. };
  133. /**
  134. * Unsubscribes the passed handler.
  135. * @param event {JitsiConnectionEvents} the connection event.
  136. * @param listener {Function} the function that will receive the event
  137. */
  138. JitsiConnection.prototype.removeEventListener = function(event, listener) {
  139. this.xmpp.removeListener(event, listener);
  140. };
  141. /**
  142. * Returns measured connectionTimes.
  143. */
  144. JitsiConnection.prototype.getConnectionTimes = function() {
  145. return this.xmpp.connectionTimes;
  146. };
  147. /**
  148. * Adds new feature to the list of supported features for the local
  149. * participant.
  150. * @param {String} feature the name of the feature.
  151. * @param {boolean} submit if true - the new list of features will be
  152. * immediately submitted to the others.
  153. */
  154. JitsiConnection.prototype.addFeature = function(feature, submit = false) {
  155. this.xmpp.caps.addFeature(feature, submit, true);
  156. };
  157. /**
  158. * Removes a feature from the list of supported features for the local
  159. * participant
  160. * @param {String} feature the name of the feature.
  161. * @param {boolean} submit if true - the new list of features will be
  162. * immediately submitted to the others.
  163. */
  164. JitsiConnection.prototype.removeFeature = function(feature, submit = false) {
  165. this.xmpp.caps.removeFeature(feature, submit, true);
  166. };
  167. /**
  168. * Get object with internal logs.
  169. */
  170. JitsiConnection.prototype.getLogs = function() {
  171. const data = this.xmpp.getJingleLog();
  172. const metadata = {};
  173. metadata.time = new Date();
  174. metadata.url = window.location.href;
  175. metadata.ua = navigator.userAgent;
  176. const log = this.xmpp.getXmppLog();
  177. if (log) {
  178. metadata.xmpp = log;
  179. }
  180. data.metadata = metadata;
  181. return data;
  182. };