Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

connection.js 4.4KB

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