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 6.7KB

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