2015-12-17 15:31:11 +00:00
|
|
|
/* global APP, JitsiMeetJS, config */
|
2016-01-06 22:39:13 +00:00
|
|
|
//FIXME:
|
|
|
|
import LoginDialog from './modules/UI/authentication/LoginDialog';
|
2015-12-17 15:31:11 +00:00
|
|
|
|
|
|
|
const ConnectionEvents = JitsiMeetJS.events.connection;
|
|
|
|
const ConnectionErrors = JitsiMeetJS.errors.connection;
|
|
|
|
|
2016-03-28 21:19:32 +00:00
|
|
|
/**
|
|
|
|
* Checks if we have data to use attach instead of connect. If we have the data
|
|
|
|
* executes attach otherwise check if we have to wait for the data. If we have
|
|
|
|
* to wait for the attach data we are setting handler to APP.connect.handler
|
|
|
|
* which is going to be called when the attach data is received otherwise
|
|
|
|
* executes connect.
|
|
|
|
*
|
|
|
|
* @param {string} [id] user id
|
|
|
|
* @param {string} [password] password
|
|
|
|
* @param {string} [roomName] the name of the conference.
|
|
|
|
*/
|
|
|
|
function checkForAttachParametersAndConnect(id, password, connection) {
|
|
|
|
if(window.XMPPAttachInfo){
|
|
|
|
APP.connect.status = "connecting";
|
2016-03-28 22:42:20 +00:00
|
|
|
// When connection optimization is not deployed or enabled the default
|
|
|
|
// value will be window.XMPPAttachInfo.status = "error"
|
|
|
|
// If the connection optimization is deployed and enabled and there is
|
|
|
|
// a failure the value will be window.XMPPAttachInfo.status = "error"
|
2016-03-28 21:19:32 +00:00
|
|
|
if(window.XMPPAttachInfo.status === "error") {
|
|
|
|
connection.connect({id, password});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var attachOptions = window.XMPPAttachInfo.data;
|
|
|
|
if(attachOptions) {
|
|
|
|
connection.attach(attachOptions);
|
|
|
|
} else {
|
|
|
|
connection.connect({id, password});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
APP.connect.status = "ready";
|
|
|
|
APP.connect.handler = checkForAttachParametersAndConnect.bind(null,
|
|
|
|
id, password, connection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 14:59:35 +00:00
|
|
|
/**
|
|
|
|
* Try to open connection using provided credentials.
|
|
|
|
* @param {string} [id]
|
|
|
|
* @param {string} [password]
|
2016-02-09 23:44:41 +00:00
|
|
|
* @param {string} [roomName]
|
2016-01-15 14:59:35 +00:00
|
|
|
* @returns {Promise<JitsiConnection>} connection if
|
|
|
|
* everything is ok, else error.
|
|
|
|
*/
|
2016-02-09 23:44:41 +00:00
|
|
|
function connect(id, password, roomName) {
|
|
|
|
|
|
|
|
let connectionConfig = config;
|
|
|
|
|
|
|
|
connectionConfig.bosh += '?room=' + roomName;
|
2016-03-28 21:19:32 +00:00
|
|
|
let connection
|
|
|
|
= new JitsiMeetJS.JitsiConnection(null, config.token, config);
|
2015-12-17 15:31:11 +00:00
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
connection.addEventListener(
|
|
|
|
ConnectionEvents.CONNECTION_ESTABLISHED, handleConnectionEstablished
|
|
|
|
);
|
|
|
|
connection.addEventListener(
|
2015-12-18 17:54:15 +00:00
|
|
|
ConnectionEvents.CONNECTION_FAILED, handleConnectionFailed
|
2015-12-17 15:31:11 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
function unsubscribe() {
|
|
|
|
connection.removeEventListener(
|
|
|
|
ConnectionEvents.CONNECTION_ESTABLISHED,
|
|
|
|
handleConnectionEstablished
|
|
|
|
);
|
|
|
|
connection.removeEventListener(
|
2015-12-18 17:54:15 +00:00
|
|
|
ConnectionEvents.CONNECTION_FAILED,
|
|
|
|
handleConnectionFailed
|
2015-12-17 15:31:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleConnectionEstablished() {
|
|
|
|
unsubscribe();
|
|
|
|
resolve(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleConnectionFailed(err) {
|
|
|
|
unsubscribe();
|
2015-12-18 17:54:15 +00:00
|
|
|
console.error("CONNECTION FAILED:", err);
|
2015-12-17 15:31:11 +00:00
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
|
2016-03-28 21:19:32 +00:00
|
|
|
checkForAttachParametersAndConnect(id, password, connection);
|
2015-12-18 17:54:15 +00:00
|
|
|
});
|
|
|
|
}
|
2015-12-17 15:31:11 +00:00
|
|
|
|
2016-01-15 14:59:35 +00:00
|
|
|
/**
|
|
|
|
* Show Authentication Dialog and try to connect with new credentials.
|
|
|
|
* If failed to connect because of PASSWORD_REQUIRED error
|
|
|
|
* then ask for password again.
|
|
|
|
* @returns {Promise<JitsiConnection>}
|
|
|
|
*/
|
2015-12-18 17:54:15 +00:00
|
|
|
function requestAuth() {
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
let authDialog = LoginDialog.showAuthDialog(
|
|
|
|
function (id, password) {
|
|
|
|
connect(id, password).then(function (connection) {
|
|
|
|
authDialog.close();
|
|
|
|
resolve(connection);
|
|
|
|
}, function (err) {
|
|
|
|
if (err === ConnectionErrors.PASSWORD_REQUIRED) {
|
|
|
|
authDialog.displayError(err);
|
|
|
|
} else {
|
|
|
|
authDialog.close();
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
});
|
2015-12-17 15:31:11 +00:00
|
|
|
}
|
2015-12-18 17:54:15 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2015-12-17 15:31:11 +00:00
|
|
|
|
2016-01-15 14:59:35 +00:00
|
|
|
/**
|
|
|
|
* Open JitsiConnection using provided credentials.
|
|
|
|
* If retry option is true it will show auth dialog on PASSWORD_REQUIRED error.
|
|
|
|
*
|
|
|
|
* @param {object} options
|
|
|
|
* @param {string} [options.id]
|
|
|
|
* @param {string} [options.password]
|
2016-02-09 23:44:41 +00:00
|
|
|
* @param {string} [options.roomName]
|
2016-01-15 14:59:35 +00:00
|
|
|
* @param {boolean} [retry] if we should show auth dialog
|
|
|
|
* on PASSWORD_REQUIRED error.
|
|
|
|
*
|
|
|
|
* @returns {Promise<JitsiConnection>}
|
|
|
|
*/
|
2016-02-09 23:44:41 +00:00
|
|
|
export function openConnection({id, password, retry, roomName}) {
|
|
|
|
return connect(id, password, roomName).catch(function (err) {
|
2015-12-18 17:54:15 +00:00
|
|
|
if (!retry) {
|
|
|
|
throw err;
|
|
|
|
}
|
2015-12-17 15:31:11 +00:00
|
|
|
|
2015-12-18 17:54:15 +00:00
|
|
|
if (err === ConnectionErrors.PASSWORD_REQUIRED) {
|
2015-12-17 15:31:11 +00:00
|
|
|
// do not retry if token is not valid
|
|
|
|
if (config.token) {
|
2015-12-18 17:54:15 +00:00
|
|
|
throw err;
|
|
|
|
} else {
|
|
|
|
return requestAuth();
|
2015-12-17 15:31:11 +00:00
|
|
|
}
|
2015-12-18 17:54:15 +00:00
|
|
|
} else {
|
|
|
|
throw err;
|
2015-12-17 15:31:11 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|