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.

connection.js 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* global APP, JitsiMeetJS, config */
  2. import LoginDialog from './UI/authentication/LoginDialog';
  3. const ConnectionEvents = JitsiMeetJS.events.connection;
  4. const ConnectionErrors = JitsiMeetJS.errors.connection;
  5. function connect(id, password) {
  6. let connection = new JitsiMeetJS.JitsiConnection(null, null, {
  7. hosts: config.hosts,
  8. bosh: config.bosh,
  9. clientNode: config.clientNode
  10. });
  11. return new Promise(function (resolve, reject) {
  12. connection.addEventListener(
  13. ConnectionEvents.CONNECTION_ESTABLISHED, handleConnectionEstablished
  14. );
  15. connection.addEventListener(
  16. ConnectionEvents.CONNECTION_FAILED, handleConnectionFailed
  17. );
  18. function unsubscribe() {
  19. connection.removeEventListener(
  20. ConnectionEvents.CONNECTION_ESTABLISHED,
  21. handleConnectionEstablished
  22. );
  23. connection.removeEventListener(
  24. ConnectionEvents.CONNECTION_FAILED,
  25. handleConnectionFailed
  26. );
  27. }
  28. function handleConnectionEstablished() {
  29. unsubscribe();
  30. resolve(connection);
  31. }
  32. function handleConnectionFailed(err) {
  33. unsubscribe();
  34. console.error("CONNECTION FAILED:", err);
  35. reject(err);
  36. }
  37. connection.connect({id, password});
  38. });
  39. }
  40. function requestAuth() {
  41. return new Promise(function (resolve, reject) {
  42. let authDialog = LoginDialog.showAuthDialog(
  43. function (id, password) {
  44. connect(id, password).then(function (connection) {
  45. authDialog.close();
  46. resolve(connection);
  47. }, function (err) {
  48. if (err === ConnectionErrors.PASSWORD_REQUIRED) {
  49. authDialog.displayError(err);
  50. } else {
  51. authDialog.close();
  52. reject(err);
  53. }
  54. });
  55. }
  56. );
  57. });
  58. }
  59. export function openConnection({id, password, retry}) {
  60. return connect(id, password).catch(function (err) {
  61. if (!retry) {
  62. throw err;
  63. }
  64. if (err === ConnectionErrors.PASSWORD_REQUIRED) {
  65. // do not retry if token is not valid
  66. if (config.token) {
  67. throw err;
  68. } else {
  69. return requestAuth();
  70. }
  71. } else {
  72. throw err;
  73. }
  74. });
  75. }