您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

JitsiConnection.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import {
  2. CONNECTION_DISCONNECTED as ANALYTICS_CONNECTION_DISCONNECTED,
  3. createConnectionFailedEvent
  4. } from './service/statistics/AnalyticsEvents';
  5. import JitsiConference from './JitsiConference';
  6. import * as JitsiConnectionEvents from './JitsiConnectionEvents';
  7. import Statistics from './modules/statistics/statistics';
  8. import XMPP from './modules/xmpp/xmpp';
  9. /**
  10. * Creates a new connection object for the Jitsi Meet server side video
  11. * conferencing service. Provides access to the JitsiConference interface.
  12. * @param appID identification for the provider of Jitsi Meet video conferencing
  13. * services.
  14. * @param token the JWT token used to authenticate with the server(optional)
  15. * @param options Object with properties / settings related to connection with
  16. * the server.
  17. * @constructor
  18. */
  19. export default function JitsiConnection(appID, token, options) {
  20. this.appID = appID;
  21. this.token = token;
  22. this.options = options;
  23. this.xmpp = new XMPP(options, token);
  24. /* eslint-disable max-params */
  25. this.addEventListener(JitsiConnectionEvents.CONNECTION_FAILED,
  26. (errType, msg, credentials, details) => {
  27. Statistics.sendAnalyticsAndLog(
  28. createConnectionFailedEvent(errType, msg, details));
  29. });
  30. /* eslint-enable max-params */
  31. this.addEventListener(JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  32. msg => {
  33. // we can see disconnects from normal tab closing of the browser
  34. // and then there are no msgs, but we want to log only disconnects
  35. // when there is real error
  36. // XXX Do we need the difference in handling between the log and
  37. // analytics event here?
  38. if (msg) {
  39. Statistics.sendAnalytics(
  40. ANALYTICS_CONNECTION_DISCONNECTED,
  41. { message: msg });
  42. }
  43. Statistics.sendLog(
  44. JSON.stringify(
  45. {
  46. id: ANALYTICS_CONNECTION_DISCONNECTED,
  47. msg
  48. }));
  49. });
  50. }
  51. /**
  52. * Connect the client with the server.
  53. * @param options {object} connecting options
  54. * (for example authentications parameters).
  55. */
  56. JitsiConnection.prototype.connect = function(options = {}) {
  57. this.xmpp.connect(options.id, options.password);
  58. };
  59. /**
  60. * Attach to existing connection. Can be used for optimizations. For example:
  61. * if the connection is created on the server we can attach to it and start
  62. * using it.
  63. *
  64. * @param options {object} connecting options - rid, sid and jid.
  65. */
  66. JitsiConnection.prototype.attach = function(options) {
  67. this.xmpp.attach(options);
  68. };
  69. /**
  70. * Disconnect the client from the server.
  71. */
  72. JitsiConnection.prototype.disconnect = function(...args) {
  73. // XXX Forward any arguments passed to JitsiConnection.disconnect to
  74. // XMPP.disconnect. For example, the caller of JitsiConnection.disconnect
  75. // may optionally pass the event which triggered the disconnect in order to
  76. // provide the implementation with finer-grained context.
  77. this.xmpp.disconnect(...args);
  78. };
  79. /**
  80. * This method allows renewal of the tokens if they are expiring.
  81. * @param token the new token.
  82. */
  83. JitsiConnection.prototype.setToken = function(token) {
  84. this.token = token;
  85. };
  86. /**
  87. * Creates and joins new conference.
  88. * @param name the name of the conference; if null - a generated name will be
  89. * provided from the api
  90. * @param options Object with properties / settings related to the conference
  91. * that will be created.
  92. * @returns {JitsiConference} returns the new conference object.
  93. */
  94. JitsiConnection.prototype.initJitsiConference = function(name, options) {
  95. return new JitsiConference({
  96. name,
  97. config: options,
  98. connection: this
  99. });
  100. };
  101. /**
  102. * Subscribes the passed listener to the event.
  103. * @param event {JitsiConnectionEvents} the connection event.
  104. * @param listener {Function} the function that will receive the event
  105. */
  106. JitsiConnection.prototype.addEventListener = function(event, listener) {
  107. this.xmpp.addListener(event, listener);
  108. };
  109. /**
  110. * Unsubscribes the passed handler.
  111. * @param event {JitsiConnectionEvents} the connection event.
  112. * @param listener {Function} the function that will receive the event
  113. */
  114. JitsiConnection.prototype.removeEventListener = function(event, listener) {
  115. this.xmpp.removeListener(event, listener);
  116. };
  117. /**
  118. * Returns measured connectionTimes.
  119. */
  120. JitsiConnection.prototype.getConnectionTimes = function() {
  121. return this.xmpp.connectionTimes;
  122. };
  123. /**
  124. * Adds new feature to the list of supported features for the local
  125. * participant.
  126. * @param {String} feature the name of the feature.
  127. * @param {boolean} submit if true - the new list of features will be
  128. * immediately submitted to the others.
  129. */
  130. JitsiConnection.prototype.addFeature = function(feature, submit = false) {
  131. return this.xmpp.caps.addFeature(feature, submit);
  132. };
  133. /**
  134. * Removes a feature from the list of supported features for the local
  135. * participant
  136. * @param {String} feature the name of the feature.
  137. * @param {boolean} submit if true - the new list of features will be
  138. * immediately submitted to the others.
  139. */
  140. JitsiConnection.prototype.removeFeature = function(feature, submit = false) {
  141. return this.xmpp.caps.removeFeature(feature, submit);
  142. };