Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

JitsiConnection.js 5.8KB

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