您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

connection.js 3.2KB

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