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

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