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 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. * Checks if we have data to use attach instead of connect. If we have the data
  8. * executes attach otherwise check if we have to wait for the data. If we have
  9. * to wait for the attach data we are setting handler to APP.connect.handler
  10. * which is going to be called when the attach data is received otherwise
  11. * executes connect.
  12. *
  13. * @param {string} [id] user id
  14. * @param {string} [password] password
  15. * @param {string} [roomName] the name of the conference.
  16. */
  17. function checkForAttachParametersAndConnect(id, password, connection) {
  18. console.log("call checkForAttachParametersAndConnect");
  19. if(window.XMPPAttachInfo){
  20. APP.connect.status = "connecting";
  21. if(window.XMPPAttachInfo.status === "error") {
  22. connection.connect({id, password});
  23. return;
  24. }
  25. var attachOptions = window.XMPPAttachInfo.data;
  26. if(attachOptions) {
  27. connection.attach(attachOptions);
  28. } else {
  29. connection.connect({id, password});
  30. }
  31. } else {
  32. APP.connect.status = "ready";
  33. APP.connect.handler = checkForAttachParametersAndConnect.bind(null,
  34. id, password, connection);
  35. }
  36. }
  37. /**
  38. * Try to open connection using provided credentials.
  39. * @param {string} [id]
  40. * @param {string} [password]
  41. * @param {string} [roomName]
  42. * @returns {Promise<JitsiConnection>} connection if
  43. * everything is ok, else error.
  44. */
  45. function connect(id, password, roomName) {
  46. let connectionConfig = config;
  47. connectionConfig.bosh += '?room=' + roomName;
  48. let connection
  49. = new JitsiMeetJS.JitsiConnection(null, config.token, config);
  50. return new Promise(function (resolve, reject) {
  51. connection.addEventListener(
  52. ConnectionEvents.CONNECTION_ESTABLISHED, handleConnectionEstablished
  53. );
  54. connection.addEventListener(
  55. ConnectionEvents.CONNECTION_FAILED, handleConnectionFailed
  56. );
  57. function unsubscribe() {
  58. connection.removeEventListener(
  59. ConnectionEvents.CONNECTION_ESTABLISHED,
  60. handleConnectionEstablished
  61. );
  62. connection.removeEventListener(
  63. ConnectionEvents.CONNECTION_FAILED,
  64. handleConnectionFailed
  65. );
  66. }
  67. function handleConnectionEstablished() {
  68. unsubscribe();
  69. resolve(connection);
  70. }
  71. function handleConnectionFailed(err) {
  72. unsubscribe();
  73. console.error("CONNECTION FAILED:", err);
  74. reject(err);
  75. }
  76. checkForAttachParametersAndConnect(id, password, connection);
  77. });
  78. }
  79. /**
  80. * Show Authentication Dialog and try to connect with new credentials.
  81. * If failed to connect because of PASSWORD_REQUIRED error
  82. * then ask for password again.
  83. * @returns {Promise<JitsiConnection>}
  84. */
  85. function requestAuth() {
  86. return new Promise(function (resolve, reject) {
  87. let authDialog = LoginDialog.showAuthDialog(
  88. function (id, password) {
  89. connect(id, password).then(function (connection) {
  90. authDialog.close();
  91. resolve(connection);
  92. }, function (err) {
  93. if (err === ConnectionErrors.PASSWORD_REQUIRED) {
  94. authDialog.displayError(err);
  95. } else {
  96. authDialog.close();
  97. reject(err);
  98. }
  99. });
  100. }
  101. );
  102. });
  103. }
  104. /**
  105. * Open JitsiConnection using provided credentials.
  106. * If retry option is true it will show auth dialog on PASSWORD_REQUIRED error.
  107. *
  108. * @param {object} options
  109. * @param {string} [options.id]
  110. * @param {string} [options.password]
  111. * @param {string} [options.roomName]
  112. * @param {boolean} [retry] if we should show auth dialog
  113. * on PASSWORD_REQUIRED error.
  114. *
  115. * @returns {Promise<JitsiConnection>}
  116. */
  117. export function openConnection({id, password, retry, roomName}) {
  118. return connect(id, password, roomName).catch(function (err) {
  119. if (!retry) {
  120. throw err;
  121. }
  122. if (err === ConnectionErrors.PASSWORD_REQUIRED) {
  123. // do not retry if token is not valid
  124. if (config.token) {
  125. throw err;
  126. } else {
  127. return requestAuth();
  128. }
  129. } else {
  130. throw err;
  131. }
  132. });
  133. }