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.

JitsiConnection.js 7.0KB

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