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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. var JitsiConference = require("./JitsiConference");
  2. import * as JitsiConnectionEvents from "./JitsiConnectionEvents";
  3. import XMPP from "./modules/xmpp/xmpp";
  4. var Statistics = require("./modules/statistics/statistics");
  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.addEventListener(JitsiConnectionEvents.CONNECTION_FAILED,
  19. function (errType, msg) {
  20. // sends analytics and callstats event
  21. Statistics.sendEventToAll('connection.failed.' + errType,
  22. {label: msg});
  23. }.bind(this));
  24. this.addEventListener(JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  25. function (msg) {
  26. // we can see disconnects from normal tab closing of the browser
  27. // and then there are no msgs, but we want to log only disconnects
  28. // when there is real error
  29. if(msg) {
  30. Statistics.analytics.sendEvent(
  31. 'connection.disconnected.' + msg);
  32. }
  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. }
  46. this.xmpp.connect(options.id, options.password);
  47. };
  48. /**
  49. * Attach to existing connection. Can be used for optimizations. For example:
  50. * if the connection is created on the server we can attach to it and start
  51. * using it.
  52. *
  53. * @param options {object} connecting options - rid, sid and jid.
  54. */
  55. JitsiConnection.prototype.attach = function (options) {
  56. this.xmpp.attach(options);
  57. };
  58. /**
  59. * Disconnect the client from the server.
  60. */
  61. JitsiConnection.prototype.disconnect = function () {
  62. // XXX Forward any arguments passed to JitsiConnection.disconnect to
  63. // XMPP.disconnect. For example, the caller of JitsiConnection.disconnect
  64. // may optionally pass the event which triggered the disconnect in order to
  65. // provide the implementation with finer-grained context.
  66. var x = this.xmpp;
  67. x.disconnect(...arguments);
  68. };
  69. /**
  70. * This method allows renewal of the tokens if they are expiring.
  71. * @param token the new token.
  72. */
  73. JitsiConnection.prototype.setToken = function (token) {
  74. this.token = token;
  75. };
  76. /**
  77. * Creates and joins new conference.
  78. * @param name the name of the conference; if null - a generated name will be
  79. * provided from the api
  80. * @param options Object with properties / settings related to the conference
  81. * that will be created.
  82. * @returns {JitsiConference} returns the new conference object.
  83. */
  84. JitsiConnection.prototype.initJitsiConference = function (name, options) {
  85. return new JitsiConference({name: name, config: options, connection: this});
  86. };
  87. /**
  88. * Subscribes the passed listener to the event.
  89. * @param event {JitsiConnectionEvents} the connection event.
  90. * @param listener {Function} the function that will receive the event
  91. */
  92. JitsiConnection.prototype.addEventListener = function (event, listener) {
  93. this.xmpp.addListener(event, listener);
  94. };
  95. /**
  96. * Unsubscribes the passed handler.
  97. * @param event {JitsiConnectionEvents} the connection event.
  98. * @param listener {Function} the function that will receive the event
  99. */
  100. JitsiConnection.prototype.removeEventListener = function (event, listener) {
  101. this.xmpp.removeListener(event, listener);
  102. };
  103. /**
  104. * Returns measured connectionTimes.
  105. */
  106. JitsiConnection.prototype.getConnectionTimes = function () {
  107. return this.xmpp.connectionTimes;
  108. };
  109. /**
  110. * Adds new feature to the list of supported features for the local
  111. * participant.
  112. * @param {String} feature the name of the feature.
  113. * @param {boolean} submit if true - the new list of features will be
  114. * immediately submitted to the others.
  115. */
  116. JitsiConnection.prototype.addFeature = function(feature, submit = false) {
  117. return this.xmpp.caps.addFeature(feature, submit);
  118. };
  119. /**
  120. * Removes a feature from the list of supported features for the local
  121. * participant
  122. * @param {String} feature the name of the feature.
  123. * @param {boolean} submit if true - the new list of features will be
  124. * immediately submitted to the others.
  125. */
  126. JitsiConnection.prototype.removeFeature = function (feature, submit = false) {
  127. return this.xmpp.caps.removeFeature(feature, submit);
  128. };
  129. module.exports = JitsiConnection;