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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import {
  2. CONNECTION_DISCONNECTED as ANALYTICS_CONNECTION_DISCONNECTED,
  3. createConnectionFailedEvent
  4. } from './service/statistics/AnalyticsEvents';
  5. import JitsiConference from './JitsiConference';
  6. import * as JitsiConnectionEvents from './JitsiConnectionEvents';
  7. import Statistics from './modules/statistics/statistics';
  8. import XMPP from './modules/xmpp/xmpp';
  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. this.addEventListener(JitsiConnectionEvents.CONNECTION_FAILED,
  25. (errType, msg) => {
  26. Statistics.sendAnalyticsAndLog(
  27. createConnectionFailedEvent(errType, msg));
  28. });
  29. this.addEventListener(JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  30. msg => {
  31. // we can see disconnects from normal tab closing of the browser
  32. // and then there are no msgs, but we want to log only disconnects
  33. // when there is real error
  34. // XXX Do we need the difference in handling between the log and
  35. // analytics event here?
  36. if (msg) {
  37. Statistics.sendAnalytics(
  38. ANALYTICS_CONNECTION_DISCONNECTED,
  39. { message: msg });
  40. }
  41. Statistics.sendLog(
  42. JSON.stringify(
  43. {
  44. id: ANALYTICS_CONNECTION_DISCONNECTED,
  45. msg
  46. }));
  47. });
  48. }
  49. /**
  50. * Connect the client with the server.
  51. * @param options {object} connecting options
  52. * (for example authentications parameters).
  53. */
  54. JitsiConnection.prototype.connect = function(options = {}) {
  55. this.xmpp.connect(options.id, options.password);
  56. };
  57. /**
  58. * Attach to existing connection. Can be used for optimizations. For example:
  59. * if the connection is created on the server we can attach to it and start
  60. * using it.
  61. *
  62. * @param options {object} connecting options - rid, sid and jid.
  63. */
  64. JitsiConnection.prototype.attach = function(options) {
  65. this.xmpp.attach(options);
  66. };
  67. /**
  68. * Disconnect the client from the server.
  69. */
  70. JitsiConnection.prototype.disconnect = function(...args) {
  71. // XXX Forward any arguments passed to JitsiConnection.disconnect to
  72. // XMPP.disconnect. For example, the caller of JitsiConnection.disconnect
  73. // may optionally pass the event which triggered the disconnect in order to
  74. // provide the implementation with finer-grained context.
  75. this.xmpp.disconnect(...args);
  76. };
  77. /**
  78. * This method allows renewal of the tokens if they are expiring.
  79. * @param token the new token.
  80. */
  81. JitsiConnection.prototype.setToken = function(token) {
  82. this.token = token;
  83. };
  84. /**
  85. * Creates and joins new conference.
  86. * @param name the name of the conference; if null - a generated name will be
  87. * provided from the api
  88. * @param options Object with properties / settings related to the conference
  89. * that will be created.
  90. * @returns {JitsiConference} returns the new conference object.
  91. */
  92. JitsiConnection.prototype.initJitsiConference = function(name, options) {
  93. return new JitsiConference({
  94. name,
  95. config: options,
  96. connection: this
  97. });
  98. };
  99. /**
  100. * Subscribes the passed listener to the event.
  101. * @param event {JitsiConnectionEvents} the connection event.
  102. * @param listener {Function} the function that will receive the event
  103. */
  104. JitsiConnection.prototype.addEventListener = function(event, listener) {
  105. this.xmpp.addListener(event, listener);
  106. };
  107. /**
  108. * Unsubscribes the passed handler.
  109. * @param event {JitsiConnectionEvents} the connection event.
  110. * @param listener {Function} the function that will receive the event
  111. */
  112. JitsiConnection.prototype.removeEventListener = function(event, listener) {
  113. this.xmpp.removeListener(event, listener);
  114. };
  115. /**
  116. * Returns measured connectionTimes.
  117. */
  118. JitsiConnection.prototype.getConnectionTimes = function() {
  119. return this.xmpp.connectionTimes;
  120. };
  121. /**
  122. * Adds new feature to the list of supported features for the local
  123. * participant.
  124. * @param {String} feature the name of the feature.
  125. * @param {boolean} submit if true - the new list of features will be
  126. * immediately submitted to the others.
  127. */
  128. JitsiConnection.prototype.addFeature = function(feature, submit = false) {
  129. return this.xmpp.caps.addFeature(feature, submit);
  130. };
  131. /**
  132. * Removes a feature from the list of supported features for the local
  133. * participant
  134. * @param {String} feature the name of the feature.
  135. * @param {boolean} submit if true - the new list of features will be
  136. * immediately submitted to the others.
  137. */
  138. JitsiConnection.prototype.removeFeature = function(feature, submit = false) {
  139. return this.xmpp.caps.removeFeature(feature, submit);
  140. };