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.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. export function openConnection({retry, 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, onConnectionFailed
  17. );
  18. let authDialog;
  19. function unsubscribe() {
  20. connection.removeEventListener(
  21. ConnectionEvents.CONNECTION_ESTABLISHED,
  22. handleConnectionEstablished
  23. );
  24. connection.removeEventListener(
  25. ConnectionEvents.CONNECTION_FAILED, onConnectionFailed
  26. );
  27. if (authDialog) {
  28. authDialog.close();
  29. }
  30. }
  31. function handleConnectionEstablished() {
  32. unsubscribe();
  33. resolve(connection);
  34. }
  35. function handleConnectionFailed(err) {
  36. unsubscribe();
  37. reject(err);
  38. }
  39. function onConnectionFailed (err) {
  40. console.error("CONNECTION FAILED:", err);
  41. if (!retry) {
  42. handleConnectionFailed(err);
  43. return;
  44. }
  45. // retry only if auth failed
  46. if (err !== ConnectionErrors.PASSWORD_REQUIRED) {
  47. handleConnectionFailed(err);
  48. return;
  49. }
  50. // do not retry if token is not valid
  51. if (config.token) {
  52. handleConnectionFailed(err);
  53. return;
  54. }
  55. // ask for password and try again
  56. if (authDialog) {
  57. authDialog.displayError(err);
  58. return;
  59. }
  60. authDialog = LoginDialog.showAuthDialog(
  61. function (id, password) {
  62. connection.connect({id, password});
  63. }
  64. );
  65. }
  66. connection.connect(id, password);
  67. });
  68. }