Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

connection.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. /**
  7. * Try to open connection using provided credentials.
  8. * @param {string} [id]
  9. * @param {string} [password]
  10. * @param {string} [roomName]
  11. * @returns {Promise<JitsiConnection>} connection if
  12. * everything is ok, else error.
  13. */
  14. function connect(id, password, roomName) {
  15. let connectionConfig = config;
  16. connectionConfig.bosh += '?room=' + roomName;
  17. let connection = new JitsiMeetJS.JitsiConnection(null, null, config);
  18. return new Promise(function (resolve, reject) {
  19. connection.addEventListener(
  20. ConnectionEvents.CONNECTION_ESTABLISHED, handleConnectionEstablished
  21. );
  22. connection.addEventListener(
  23. ConnectionEvents.CONNECTION_FAILED, handleConnectionFailed
  24. );
  25. function unsubscribe() {
  26. connection.removeEventListener(
  27. ConnectionEvents.CONNECTION_ESTABLISHED,
  28. handleConnectionEstablished
  29. );
  30. connection.removeEventListener(
  31. ConnectionEvents.CONNECTION_FAILED,
  32. handleConnectionFailed
  33. );
  34. }
  35. function handleConnectionEstablished() {
  36. unsubscribe();
  37. resolve(connection);
  38. }
  39. function handleConnectionFailed(err) {
  40. unsubscribe();
  41. console.error("CONNECTION FAILED:", err);
  42. reject(err);
  43. }
  44. connection.connect({id, password});
  45. });
  46. }
  47. /**
  48. * Show Authentication Dialog and try to connect with new credentials.
  49. * If failed to connect because of PASSWORD_REQUIRED error
  50. * then ask for password again.
  51. * @returns {Promise<JitsiConnection>}
  52. */
  53. function requestAuth() {
  54. return new Promise(function (resolve, reject) {
  55. let authDialog = LoginDialog.showAuthDialog(
  56. function (id, password) {
  57. connect(id, password).then(function (connection) {
  58. authDialog.close();
  59. resolve(connection);
  60. }, function (err) {
  61. if (err === ConnectionErrors.PASSWORD_REQUIRED) {
  62. authDialog.displayError(err);
  63. } else {
  64. authDialog.close();
  65. reject(err);
  66. }
  67. });
  68. }
  69. );
  70. });
  71. }
  72. /**
  73. * Open JitsiConnection using provided credentials.
  74. * If retry option is true it will show auth dialog on PASSWORD_REQUIRED error.
  75. *
  76. * @param {object} options
  77. * @param {string} [options.id]
  78. * @param {string} [options.password]
  79. * @param {string} [options.roomName]
  80. * @param {boolean} [retry] if we should show auth dialog
  81. * on PASSWORD_REQUIRED error.
  82. *
  83. * @returns {Promise<JitsiConnection>}
  84. */
  85. export function openConnection({id, password, retry, roomName}) {
  86. return connect(id, password, roomName).catch(function (err) {
  87. if (!retry) {
  88. throw err;
  89. }
  90. if (err === ConnectionErrors.PASSWORD_REQUIRED) {
  91. // do not retry if token is not valid
  92. if (config.token) {
  93. throw err;
  94. } else {
  95. return requestAuth();
  96. }
  97. } else {
  98. throw err;
  99. }
  100. });
  101. }