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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. var JitsiConference = require("./JitsiConference");
  2. var XMPP = require("./modules/xmpp/xmpp");
  3. var JitsiConnectionEvents = require("./JitsiConnectionEvents");
  4. var JitsiConnectionErrors = require("./JitsiConnectionErrors");
  5. var AnalyticsAdapter = require("./modules/statistics/AnalyticsAdapter");
  6. /**
  7. * Creates new connection object for the Jitsi Meet server side video conferencing service. Provides access to the
  8. * JitsiConference interface.
  9. * @param appID identification for the provider of Jitsi Meet video conferencing 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 the server.
  12. * @constructor
  13. */
  14. function JitsiConnection(appID, token, options) {
  15. this.appID = appID;
  16. this.token = token;
  17. this.options = options;
  18. this.xmpp = new XMPP(options, token);
  19. this.conferences = {};
  20. this.retryOnFail = 0;
  21. this.addEventListener(JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  22. function () {
  23. this.retryOnFail = 3;
  24. }.bind(this));
  25. this.addEventListener(JitsiConnectionEvents.CONNECTION_FAILED,
  26. function (errType, msg) {
  27. AnalyticsAdapter.sendEvent('connection.failed.' + errType);
  28. if(errType === JitsiConnectionErrors.OTHER_ERROR &&
  29. (msg === "item-not-found" || msg === "host-unknown")) {
  30. // FIXME: don't report the error if we are going to reload
  31. this._reload();
  32. }
  33. }.bind(this));
  34. this.addEventListener(JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  35. function (msg) {
  36. // we can see disconnects from normal tab closing of the browser
  37. // and then there are no msgs, but we want to log only disconnects
  38. // when there is real error
  39. if(msg)
  40. AnalyticsAdapter.sendEvent('connection.disconnected.' + msg);
  41. });
  42. }
  43. /**
  44. * Connect the client with the server.
  45. * @param options {object} connecting options
  46. * (for example authentications parameters).
  47. */
  48. JitsiConnection.prototype.connect = function (options) {
  49. if(!options)
  50. options = {};
  51. this.xmpp.connect(options.id, options.password);
  52. }
  53. /**
  54. * Attach to existing connection. Can be used for optimizations. For example:
  55. * if the connection is created on the server we can attach to it and start
  56. * using it.
  57. *
  58. * @param options {object} connecting options - rid, sid and jid.
  59. */
  60. JitsiConnection.prototype.attach = function (options) {
  61. this.xmpp.attach(options);
  62. }
  63. /**
  64. * Reloads the JitsiConnection instance and all related conferences
  65. */
  66. JitsiConnection.prototype._reload = function () {
  67. if(this.retryOnFail === 0)
  68. return false;
  69. AnalyticsAdapter.sendEvent('connection.reload');
  70. this.retryOnFail--;
  71. var states = {};
  72. for(var name in this.conferences) {
  73. states[name] = this.conferences[name].room.exportState();
  74. this.conferences[name].leave(true);
  75. }
  76. this.connectionEstablishedHandler =
  77. this._reloadConferences.bind(this, states);
  78. this.addEventListener(JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  79. this.connectionEstablishedHandler);
  80. this.xmpp.reload();
  81. return true;
  82. }
  83. /**
  84. * Reloads all conferences related to this JitsiConnection instance
  85. * @param states {object} the exported states per conference
  86. */
  87. JitsiConnection.prototype._reloadConferences = function (states) {
  88. this.removeEventListener(JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  89. this.connectionEstablishedHandler);
  90. this.connectionEstablishedHandler = null;
  91. states = states || {};
  92. for(var name in this.conferences) {
  93. this.conferences[name]._init({roomState: states[name]});
  94. this.conferences[name].join();
  95. }
  96. }
  97. /**
  98. * Disconnect the client from the server.
  99. */
  100. JitsiConnection.prototype.disconnect = function () {
  101. // XXX Forward any arguments passed to JitsiConnection.disconnect to
  102. // XMPP.disconnect. For example, the caller of JitsiConnection.disconnect
  103. // may optionally pass the event which triggered the disconnect in order to
  104. // provide the implementation with finer-grained context.
  105. var x = this.xmpp;
  106. x.disconnect.apply(x, arguments);
  107. }
  108. /**
  109. * This method allows renewal of the tokens if they are expiring.
  110. * @param token the new token.
  111. */
  112. JitsiConnection.prototype.setToken = function (token) {
  113. this.token = token;
  114. }
  115. /**
  116. * Creates and joins new conference.
  117. * @param name the name of the conference; if null - a generated name will be
  118. * provided from the api
  119. * @param options Object with properties / settings related to the conference
  120. * that will be created.
  121. * @returns {JitsiConference} returns the new conference object.
  122. */
  123. JitsiConnection.prototype.initJitsiConference = function (name, options) {
  124. var conference
  125. = new JitsiConference({name: name, config: options, connection: this});
  126. this.conferences[name] = conference;
  127. return conference;
  128. }
  129. /**
  130. * Subscribes the passed listener to the event.
  131. * @param event {JitsiConnectionEvents} the connection event.
  132. * @param listener {Function} the function that will receive the event
  133. */
  134. JitsiConnection.prototype.addEventListener = function (event, listener) {
  135. this.xmpp.addListener(event, listener);
  136. }
  137. /**
  138. * Unsubscribes the passed handler.
  139. * @param event {JitsiConnectionEvents} the connection event.
  140. * @param listener {Function} the function that will receive the event
  141. */
  142. JitsiConnection.prototype.removeEventListener = function (event, listener) {
  143. this.xmpp.removeListener(event, listener);
  144. }
  145. /**
  146. * Returns measured connectionTimes.
  147. */
  148. JitsiConnection.prototype.getConnectionTimes = function () {
  149. return this.xmpp.connectionTimes;
  150. };
  151. module.exports = JitsiConnection;