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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. if(window.XMPPAttachInfo){
  19. APP.connect.status = "connecting";
  20. // When connection optimization is not deployed or enabled the default
  21. // value will be window.XMPPAttachInfo.status = "error"
  22. // If the connection optimization is deployed and enabled and there is
  23. // a failure the value will be window.XMPPAttachInfo.status = "error"
  24. if(window.XMPPAttachInfo.status === "error") {
  25. connection.connect({id, password});
  26. return;
  27. }
  28. var attachOptions = window.XMPPAttachInfo.data;
  29. if(attachOptions) {
  30. connection.attach(attachOptions);
  31. delete window.XMPPAttachInfo.data;
  32. } else {
  33. connection.connect({id, password});
  34. }
  35. } else {
  36. APP.connect.status = "ready";
  37. APP.connect.handler = checkForAttachParametersAndConnect.bind(null,
  38. id, password, connection);
  39. }
  40. }
  41. /**
  42. * Try to open connection using provided credentials.
  43. * @param {string} [id]
  44. * @param {string} [password]
  45. * @param {string} [roomName]
  46. * @returns {Promise<JitsiConnection>} connection if
  47. * everything is ok, else error.
  48. */
  49. function connect(id, password, roomName) {
  50. let connectionConfig = Object.assign({}, config);
  51. connectionConfig.bosh += '?room=' + roomName;
  52. let connection
  53. = new JitsiMeetJS.JitsiConnection(null, config.token, connectionConfig);
  54. return new Promise(function (resolve, reject) {
  55. connection.addEventListener(
  56. ConnectionEvents.CONNECTION_ESTABLISHED, handleConnectionEstablished
  57. );
  58. connection.addEventListener(
  59. ConnectionEvents.CONNECTION_FAILED, handleConnectionFailed
  60. );
  61. function unsubscribe() {
  62. connection.removeEventListener(
  63. ConnectionEvents.CONNECTION_ESTABLISHED,
  64. handleConnectionEstablished
  65. );
  66. connection.removeEventListener(
  67. ConnectionEvents.CONNECTION_FAILED,
  68. handleConnectionFailed
  69. );
  70. }
  71. function handleConnectionEstablished() {
  72. unsubscribe();
  73. resolve(connection);
  74. }
  75. function handleConnectionFailed(err) {
  76. unsubscribe();
  77. console.error("CONNECTION FAILED:", err);
  78. reject(err);
  79. }
  80. checkForAttachParametersAndConnect(id, password, connection);
  81. });
  82. }
  83. /**
  84. * Show Authentication Dialog and try to connect with new credentials.
  85. * If failed to connect because of PASSWORD_REQUIRED error
  86. * then ask for password again.
  87. * @param {string} [roomName]
  88. * @returns {Promise<JitsiConnection>}
  89. */
  90. function requestAuth(roomName) {
  91. return new Promise(function (resolve, reject) {
  92. let authDialog = LoginDialog.showAuthDialog(
  93. function (id, password) {
  94. connect(id, password, roomName).then(function (connection) {
  95. authDialog.close();
  96. resolve(connection);
  97. }, function (err) {
  98. if (err === ConnectionErrors.PASSWORD_REQUIRED) {
  99. authDialog.displayError(err);
  100. } else {
  101. authDialog.close();
  102. reject(err);
  103. }
  104. });
  105. }
  106. );
  107. });
  108. }
  109. /**
  110. * Open JitsiConnection using provided credentials.
  111. * If retry option is true it will show auth dialog on PASSWORD_REQUIRED error.
  112. *
  113. * @param {object} options
  114. * @param {string} [options.id]
  115. * @param {string} [options.password]
  116. * @param {string} [options.roomName]
  117. * @param {boolean} [retry] if we should show auth dialog
  118. * on PASSWORD_REQUIRED error.
  119. *
  120. * @returns {Promise<JitsiConnection>}
  121. */
  122. export function openConnection({id, password, retry, roomName}) {
  123. let usernameOverride
  124. = window.localStorage.getItem("xmpp_username_override");
  125. let passwordOverride
  126. = window.localStorage.getItem("xmpp_password_override");
  127. if (usernameOverride && usernameOverride.length > 0) {
  128. id = usernameOverride;
  129. }
  130. if (passwordOverride && passwordOverride.length > 0) {
  131. password = passwordOverride;
  132. }
  133. return connect(id, password, roomName).catch(function (err) {
  134. if (!retry) {
  135. throw err;
  136. }
  137. if (err === ConnectionErrors.PASSWORD_REQUIRED) {
  138. // do not retry if token is not valid
  139. if (config.token) {
  140. throw err;
  141. } else {
  142. return requestAuth(roomName);
  143. }
  144. } else {
  145. throw err;
  146. }
  147. });
  148. }