jiti-meet/connection.js

83 lines
2.4 KiB
JavaScript
Raw Normal View History

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;
function connect(id, password) {
2016-01-15 19:50:16 +00:00
let connection = new JitsiMeetJS.JitsiConnection(null, null, config);
2015-12-17 15:31:11 +00:00
return new Promise(function (resolve, reject) {
connection.addEventListener(
ConnectionEvents.CONNECTION_ESTABLISHED, handleConnectionEstablished
);
connection.addEventListener(
ConnectionEvents.CONNECTION_FAILED, handleConnectionFailed
2015-12-17 15:31:11 +00:00
);
function unsubscribe() {
connection.removeEventListener(
ConnectionEvents.CONNECTION_ESTABLISHED,
handleConnectionEstablished
);
connection.removeEventListener(
ConnectionEvents.CONNECTION_FAILED,
handleConnectionFailed
2015-12-17 15:31:11 +00:00
);
}
function handleConnectionEstablished() {
unsubscribe();
resolve(connection);
}
function handleConnectionFailed(err) {
unsubscribe();
console.error("CONNECTION FAILED:", err);
2015-12-17 15:31:11 +00:00
reject(err);
}
connection.connect({id, password});
});
}
2015-12-17 15:31:11 +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-17 15:31:11 +00:00
export function openConnection({id, password, retry}) {
return connect(id, password).catch(function (err) {
if (!retry) {
throw err;
}
2015-12-17 15:31:11 +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) {
throw err;
} else {
return requestAuth();
2015-12-17 15:31:11 +00:00
}
} else {
throw err;
2015-12-17 15:31:11 +00:00
}
});
}