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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. var JitsiConference = require("./JitsiConference");
  2. import * as JitsiConnectionErrors from "./JitsiConnectionErrors";
  3. import * as JitsiConnectionEvents from "./JitsiConnectionEvents";
  4. import XMPP from "./modules/xmpp/xmpp";
  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.addEventListener(JitsiConnectionEvents.CONNECTION_FAILED,
  21. function (errType, msg) {
  22. // sends analytics and callstats event
  23. Statistics.sendEventToAll('connection.failed.' + errType, msg);
  24. }.bind(this));
  25. this.addEventListener(JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  26. function (msg) {
  27. // we can see disconnects from normal tab closing of the browser
  28. // and then there are no msgs, but we want to log only disconnects
  29. // when there is real error
  30. if(msg)
  31. Statistics.analytics.sendEvent(
  32. 'connection.disconnected.' + msg);
  33. Statistics.sendLog(
  34. JSON.stringify({id: "connection.disconnected", msg: msg}));
  35. });
  36. }
  37. /**
  38. * Connect the client with the server.
  39. * @param options {object} connecting options
  40. * (for example authentications parameters).
  41. */
  42. JitsiConnection.prototype.connect = function (options) {
  43. if(!options)
  44. options = {};
  45. this.xmpp.connect(options.id, options.password);
  46. }
  47. /**
  48. * Attach to existing connection. Can be used for optimizations. For example:
  49. * if the connection is created on the server we can attach to it and start
  50. * using it.
  51. *
  52. * @param options {object} connecting options - rid, sid and jid.
  53. */
  54. JitsiConnection.prototype.attach = function (options) {
  55. this.xmpp.attach(options);
  56. }
  57. /**
  58. * Disconnect the client from the server.
  59. */
  60. JitsiConnection.prototype.disconnect = function () {
  61. // XXX Forward any arguments passed to JitsiConnection.disconnect to
  62. // XMPP.disconnect. For example, the caller of JitsiConnection.disconnect
  63. // may optionally pass the event which triggered the disconnect in order to
  64. // provide the implementation with finer-grained context.
  65. var x = this.xmpp;
  66. x.disconnect.apply(x, arguments);
  67. }
  68. /**
  69. * This method allows renewal of the tokens if they are expiring.
  70. * @param token the new token.
  71. */
  72. JitsiConnection.prototype.setToken = function (token) {
  73. this.token = token;
  74. }
  75. /**
  76. * Creates and joins new conference.
  77. * @param name the name of the conference; if null - a generated name will be
  78. * provided from the api
  79. * @param options Object with properties / settings related to the conference
  80. * that will be created.
  81. * @returns {JitsiConference} returns the new conference object.
  82. */
  83. JitsiConnection.prototype.initJitsiConference = function (name, options) {
  84. var conference
  85. = new JitsiConference({name: name, config: options, connection: this});
  86. this.conferences[name] = conference;
  87. return conference;
  88. }
  89. /**
  90. * Subscribes the passed listener to the event.
  91. * @param event {JitsiConnectionEvents} the connection event.
  92. * @param listener {Function} the function that will receive the event
  93. */
  94. JitsiConnection.prototype.addEventListener = function (event, listener) {
  95. this.xmpp.addListener(event, listener);
  96. }
  97. /**
  98. * Unsubscribes the passed handler.
  99. * @param event {JitsiConnectionEvents} the connection event.
  100. * @param listener {Function} the function that will receive the event
  101. */
  102. JitsiConnection.prototype.removeEventListener = function (event, listener) {
  103. this.xmpp.removeListener(event, listener);
  104. }
  105. /**
  106. * Returns measured connectionTimes.
  107. */
  108. JitsiConnection.prototype.getConnectionTimes = function () {
  109. return this.xmpp.connectionTimes;
  110. };
  111. module.exports = JitsiConnection;