Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

connection.js 4.5KB

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