jiti-meet/JitsiConnection.js

99 lines
3.4 KiB
JavaScript

var JitsiConference = require("./JitsiConference");
var XMPP = require("./modules/xmpp/xmpp");
var RandomUtil = require("./modules/util/RandomUtil");
/**
* Utility method that generates user name based on random hex values.
* Eg. 12345678-1234-1234-12345678
* @returns {string}
*/
function generateUserName() {
return RandomUtil.random8digitsHex() + "-" + RandomUtil.random4digitsHex() + "-" +
RandomUtil.random4digitsHex() + "-" + RandomUtil.random8digitsHex();
}
/**
* Creates new connection object for the Jitsi Meet server side video conferencing service. Provides access to the
* JitsiConference interface.
* @param appID identification for the provider of Jitsi Meet video conferencing services.
* @param tokenPassword secret generated by the provider of Jitsi Meet video conferencing services.
* The token will be send to the provider from the Jitsi Meet server deployment for authorization of the current client.
* The format is:
* passwordToken = token + "_" + roomName + "_" + ts
* See doc/tokens.md for more info on how tokens are generated.
* @param options Object with properties / settings related to connection with the server.
* @constructor
*/
function JitsiConnection(appID, tokenPassword, options) {
this.appID = appID;
this.tokenPassword = tokenPassword;
this.options = options;
this.xmpp = new XMPP(options);
this.conferences = {};
}
/**
* Connect the client with the server.
* @param options {object} connecting options (for example authentications parameters).
*/
JitsiConnection.prototype.connect = function (options) {
if(!options)
options = {};
// If we have token provided use it as a password and generate random username
if (this.tokenPassword) {
options.password = this.tokenPassword;
if (!options.id) {
options.id = generateUserName() + "@" + this.options.hosts.domain;
}
}
this.xmpp.connect(options.id, options.password);
}
/**
* Disconnect the client from the server.
*/
JitsiConnection.prototype.disconnect = function () {
this.xmpp.disconnect();
}
/**
* This method allows renewal of the tokens if they are expiring.
* @param token the new token.
*/
JitsiConnection.prototype.setToken = function (token) {
this.token = token;
}
/**
* Creates and joins new conference.
* @param name the name of the conference; if null - a generated name will be provided from the api
* @param options Object with properties / settings related to the conference that will be created.
* @returns {JitsiConference} returns the new conference object.
*/
JitsiConnection.prototype.initJitsiConference = function (name, options) {
this.conferences[name] = new JitsiConference({name: name, config: options, connection: this});
return this.conferences[name];
}
/**
* Subscribes the passed listener to the event.
* @param event {JitsiConnectionEvents} the connection event.
* @param listener {Function} the function that will receive the event
*/
JitsiConnection.prototype.addEventListener = function (event, listener) {
this.xmpp.addListener(event, listener);
}
/**
* Unsubscribes the passed handler.
* @param event {JitsiConnectionEvents} the connection event.
* @param listener {Function} the function that will receive the event
*/
JitsiConnection.prototype.removeEventListener = function (event, listener) {
this.xmpp.removeListener(event, listener);
}
module.exports = JitsiConnection;