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