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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* global APP, JitsiMeetJS, config */
  2. const logger = require("jitsi-meet-logger").getLogger(__filename);
  3. import AuthHandler from './modules/UI/authentication/AuthHandler';
  4. import jitsiLocalStorage from './modules/util/JitsiLocalStorage';
  5. import {
  6. connectionEstablished,
  7. connectionFailed
  8. } from './react/features/base/connection';
  9. const ConnectionEvents = JitsiMeetJS.events.connection;
  10. const ConnectionErrors = JitsiMeetJS.errors.connection;
  11. /**
  12. * Checks if we have data to use attach instead of connect. If we have the data
  13. * executes attach otherwise check if we have to wait for the data. If we have
  14. * to wait for the attach data we are setting handler to APP.connect.handler
  15. * which is going to be called when the attach data is received otherwise
  16. * executes connect.
  17. *
  18. * @param {string} [id] user id
  19. * @param {string} [password] password
  20. * @param {string} [roomName] the name of the conference.
  21. */
  22. function checkForAttachParametersAndConnect(id, password, connection) {
  23. if(window.XMPPAttachInfo){
  24. APP.connect.status = "connecting";
  25. // When connection optimization is not deployed or enabled the default
  26. // value will be window.XMPPAttachInfo.status = "error"
  27. // If the connection optimization is deployed and enabled and there is
  28. // a failure the value will be window.XMPPAttachInfo.status = "error"
  29. if(window.XMPPAttachInfo.status === "error") {
  30. connection.connect({id, password});
  31. return;
  32. }
  33. var attachOptions = window.XMPPAttachInfo.data;
  34. if(attachOptions) {
  35. connection.attach(attachOptions);
  36. delete window.XMPPAttachInfo.data;
  37. } else {
  38. connection.connect({id, password});
  39. }
  40. } else {
  41. APP.connect.status = "ready";
  42. APP.connect.handler = checkForAttachParametersAndConnect.bind(null,
  43. id, password, connection);
  44. }
  45. }
  46. /**
  47. * Try to open connection using provided credentials.
  48. * @param {string} [id]
  49. * @param {string} [password]
  50. * @param {string} [roomName]
  51. * @returns {Promise<JitsiConnection>} connection if
  52. * everything is ok, else error.
  53. */
  54. function connect(id, password, roomName) {
  55. let connectionConfig = Object.assign({}, config);
  56. connectionConfig.bosh += '?room=' + roomName;
  57. let connection
  58. = new JitsiMeetJS.JitsiConnection(null, config.token, connectionConfig);
  59. return new Promise(function (resolve, reject) {
  60. connection.addEventListener(
  61. ConnectionEvents.CONNECTION_ESTABLISHED, handleConnectionEstablished
  62. );
  63. connection.addEventListener(
  64. ConnectionEvents.CONNECTION_FAILED, handleConnectionFailed
  65. );
  66. connection.addEventListener(
  67. ConnectionEvents.CONNECTION_FAILED, connectionFailedHandler);
  68. function connectionFailedHandler (error, errMsg) {
  69. APP.store.dispatch(connectionFailed(connection, error, errMsg));
  70. switch (error) {
  71. case ConnectionErrors.CONNECTION_DROPPED_ERROR:
  72. case ConnectionErrors.OTHER_ERROR:
  73. case ConnectionErrors.SERVER_ERROR: {
  74. connection.removeEventListener(
  75. ConnectionEvents.CONNECTION_FAILED,
  76. connectionFailedHandler);
  77. break;
  78. }
  79. }
  80. }
  81. function unsubscribe() {
  82. connection.removeEventListener(
  83. ConnectionEvents.CONNECTION_ESTABLISHED,
  84. handleConnectionEstablished
  85. );
  86. connection.removeEventListener(
  87. ConnectionEvents.CONNECTION_FAILED,
  88. handleConnectionFailed
  89. );
  90. }
  91. function handleConnectionEstablished() {
  92. APP.store.dispatch(connectionEstablished(connection));
  93. unsubscribe();
  94. resolve(connection);
  95. }
  96. function handleConnectionFailed(err) {
  97. unsubscribe();
  98. logger.error("CONNECTION FAILED:", err);
  99. reject(err);
  100. }
  101. checkForAttachParametersAndConnect(id, password, connection);
  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. let usernameOverride
  119. = jitsiLocalStorage.getItem("xmpp_username_override");
  120. let passwordOverride
  121. = jitsiLocalStorage.getItem("xmpp_password_override");
  122. if (usernameOverride && usernameOverride.length > 0) {
  123. id = usernameOverride;
  124. }
  125. if (passwordOverride && passwordOverride.length > 0) {
  126. password = passwordOverride;
  127. }
  128. return connect(id, password, roomName).catch(function (err) {
  129. if (!retry) {
  130. throw err;
  131. }
  132. if (err === ConnectionErrors.PASSWORD_REQUIRED) {
  133. // do not retry if token is not valid
  134. if (config.token) {
  135. throw err;
  136. } else {
  137. return AuthHandler.requestAuth(roomName, connect);
  138. }
  139. } else {
  140. throw err;
  141. }
  142. });
  143. }