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

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