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

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