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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. Statistics.sendLog(
  44. JSON.stringify(
  45. {
  46. id: ANALYTICS_CONNECTION_DISCONNECTED,
  47. 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. // if we get redirected, we set disableFocus to skip sending the conference request twice
  63. if (this.xmpp.moderator.targetUrl && !this.options.disableFocus && options.name) {
  64. this.xmpp.moderator.sendConferenceRequest(this.xmpp.getRoomJid(options.name))
  65. .then(() => {
  66. this.xmpp.connect(options.id, options.password);
  67. });
  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. };