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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. var JitsiConference = require("./JitsiConference");
  2. var XMPP = require("./modules/xmpp/xmpp");
  3. var JitsiConnectionEvents = require("./JitsiConnectionEvents");
  4. var JitsiConnectionErrors = require("./JitsiConnectionErrors");
  5. var Statistics = require("./modules/statistics/statistics");
  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. Statistics.analytics.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. Statistics.analytics.sendEvent(
  41. 'connection.disconnected.' + 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. if(!options)
  51. options = {};
  52. this.xmpp.connect(options.id, options.password);
  53. }
  54. /**
  55. * Attach to existing connection. Can be used for optimizations. For example:
  56. * if the connection is created on the server we can attach to it and start
  57. * using it.
  58. *
  59. * @param options {object} connecting options - rid, sid and jid.
  60. */
  61. JitsiConnection.prototype.attach = function (options) {
  62. this.xmpp.attach(options);
  63. }
  64. /**
  65. * Reloads the JitsiConnection instance and all related conferences
  66. */
  67. JitsiConnection.prototype._reload = function () {
  68. if(this.retryOnFail === 0)
  69. return false;
  70. Statistics.analytics.sendEvent('connection.reload');
  71. this.retryOnFail--;
  72. var states = {};
  73. for(var name in this.conferences) {
  74. states[name] = this.conferences[name].room.exportState();
  75. this.conferences[name].leave(true);
  76. }
  77. this.connectionEstablishedHandler =
  78. this._reloadConferences.bind(this, states);
  79. this.addEventListener(JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  80. this.connectionEstablishedHandler);
  81. this.xmpp.reload();
  82. return true;
  83. }
  84. /**
  85. * Reloads all conferences related to this JitsiConnection instance
  86. * @param states {object} the exported states per conference
  87. */
  88. JitsiConnection.prototype._reloadConferences = function (states) {
  89. this.removeEventListener(JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  90. this.connectionEstablishedHandler);
  91. this.connectionEstablishedHandler = null;
  92. states = states || {};
  93. for(var name in this.conferences) {
  94. this.conferences[name]._init({roomState: states[name]});
  95. this.conferences[name].join();
  96. }
  97. }
  98. /**
  99. * Disconnect the client from the server.
  100. */
  101. JitsiConnection.prototype.disconnect = function () {
  102. // XXX Forward any arguments passed to JitsiConnection.disconnect to
  103. // XMPP.disconnect. For example, the caller of JitsiConnection.disconnect
  104. // may optionally pass the event which triggered the disconnect in order to
  105. // provide the implementation with finer-grained context.
  106. var x = this.xmpp;
  107. x.disconnect.apply(x, arguments);
  108. }
  109. /**
  110. * This method allows renewal of the tokens if they are expiring.
  111. * @param token the new token.
  112. */
  113. JitsiConnection.prototype.setToken = function (token) {
  114. this.token = token;
  115. }
  116. /**
  117. * Creates and joins new conference.
  118. * @param name the name of the conference; if null - a generated name will be
  119. * provided from the api
  120. * @param options Object with properties / settings related to the conference
  121. * that will be created.
  122. * @returns {JitsiConference} returns the new conference object.
  123. */
  124. JitsiConnection.prototype.initJitsiConference = function (name, options) {
  125. var conference
  126. = new JitsiConference({name: name, config: options, connection: this});
  127. this.conferences[name] = conference;
  128. return conference;
  129. }
  130. /**
  131. * Subscribes the passed listener to the event.
  132. * @param event {JitsiConnectionEvents} the connection event.
  133. * @param listener {Function} the function that will receive the event
  134. */
  135. JitsiConnection.prototype.addEventListener = function (event, listener) {
  136. this.xmpp.addListener(event, listener);
  137. }
  138. /**
  139. * Unsubscribes the passed handler.
  140. * @param event {JitsiConnectionEvents} the connection event.
  141. * @param listener {Function} the function that will receive the event
  142. */
  143. JitsiConnection.prototype.removeEventListener = function (event, listener) {
  144. this.xmpp.removeListener(event, listener);
  145. }
  146. /**
  147. * Returns measured connectionTimes.
  148. */
  149. JitsiConnection.prototype.getConnectionTimes = function () {
  150. return this.xmpp.connectionTimes;
  151. };
  152. module.exports = JitsiConnection;