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

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