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 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. var JitsiConference = require("./JitsiConference");
  2. var XMPP = require("./modules/xmpp/xmpp");
  3. var RandomUtil = require("./modules/util/RandomUtil");
  4. /**
  5. * Utility method that generates user name based on random hex values.
  6. * Eg. 12345678-1234-1234-12345678
  7. * @returns {string}
  8. */
  9. function generateUserName() {
  10. return RandomUtil.randomHexString(8) + "-" + RandomUtil.randomHexString(4) +
  11. "-" + RandomUtil.randomHexString(4) + "-" +
  12. RandomUtil.randomHexDigit(8);
  13. }
  14. /**
  15. * Creates new connection object for the Jitsi Meet server side video conferencing service. Provides access to the
  16. * JitsiConference interface.
  17. * @param appID identification for the provider of Jitsi Meet video conferencing services.
  18. * @param tokenPassword secret generated by the provider of Jitsi Meet video conferencing services.
  19. * The token will be send to the provider from the Jitsi Meet server deployment for authorization of the current client.
  20. * The format is:
  21. * passwordToken = token + "_" + roomName + "_" + ts
  22. * See doc/tokens.md for more info on how tokens are generated.
  23. * @param options Object with properties / settings related to connection with the server.
  24. * @constructor
  25. */
  26. function JitsiConnection(appID, tokenPassword, options) {
  27. this.appID = appID;
  28. this.tokenPassword = tokenPassword;
  29. this.options = options;
  30. this.xmpp = new XMPP(options);
  31. this.conferences = {};
  32. }
  33. /**
  34. * Connect the client with the server.
  35. * @param options {object} connecting options (for example authentications parameters).
  36. */
  37. JitsiConnection.prototype.connect = function (options) {
  38. if(!options)
  39. options = {};
  40. // If we have token provided use it as a password and generate random username
  41. if (this.tokenPassword) {
  42. options.password = this.tokenPassword;
  43. if (!options.id) {
  44. options.id = generateUserName() + "@" + this.options.hosts.domain;
  45. }
  46. }
  47. this.xmpp.connect(options.id, options.password);
  48. }
  49. /**
  50. * Disconnect the client from the server.
  51. */
  52. JitsiConnection.prototype.disconnect = function () {
  53. this.xmpp.disconnect();
  54. }
  55. /**
  56. * This method allows renewal of the tokens if they are expiring.
  57. * @param token the new token.
  58. */
  59. JitsiConnection.prototype.setToken = function (token) {
  60. this.token = token;
  61. }
  62. /**
  63. * Creates and joins new conference.
  64. * @param name the name of the conference; if null - a generated name will be provided from the api
  65. * @param options Object with properties / settings related to the conference that will be created.
  66. * @returns {JitsiConference} returns the new conference object.
  67. */
  68. JitsiConnection.prototype.initJitsiConference = function (name, options) {
  69. this.conferences[name] = new JitsiConference({name: name, config: options, connection: this});
  70. return this.conferences[name];
  71. }
  72. /**
  73. * Subscribes the passed listener to the event.
  74. * @param event {JitsiConnectionEvents} the connection event.
  75. * @param listener {Function} the function that will receive the event
  76. */
  77. JitsiConnection.prototype.addEventListener = function (event, listener) {
  78. this.xmpp.addListener(event, listener);
  79. }
  80. /**
  81. * Unsubscribes the passed handler.
  82. * @param event {JitsiConnectionEvents} the connection event.
  83. * @param listener {Function} the function that will receive the event
  84. */
  85. JitsiConnection.prototype.removeEventListener = function (event, listener) {
  86. this.xmpp.removeListener(event, listener);
  87. }
  88. module.exports = JitsiConnection;