您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

connection.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. } 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 = config;
  50. connectionConfig.bosh += '?room=' + roomName;
  51. let connection
  52. = new JitsiMeetJS.JitsiConnection(null, config.token, config);
  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. * Show Authentication Dialog and try to connect with new credentials.
  84. * If failed to connect because of PASSWORD_REQUIRED error
  85. * then ask for password again.
  86. * @returns {Promise<JitsiConnection>}
  87. */
  88. function requestAuth() {
  89. return new Promise(function (resolve, reject) {
  90. let authDialog = LoginDialog.showAuthDialog(
  91. function (id, password) {
  92. connect(id, password).then(function (connection) {
  93. authDialog.close();
  94. resolve(connection);
  95. }, function (err) {
  96. if (err === ConnectionErrors.PASSWORD_REQUIRED) {
  97. authDialog.displayError(err);
  98. } else {
  99. authDialog.close();
  100. reject(err);
  101. }
  102. });
  103. }
  104. );
  105. });
  106. }
  107. /**
  108. * Open JitsiConnection using provided credentials.
  109. * If retry option is true it will show auth dialog on PASSWORD_REQUIRED error.
  110. *
  111. * @param {object} options
  112. * @param {string} [options.id]
  113. * @param {string} [options.password]
  114. * @param {string} [options.roomName]
  115. * @param {boolean} [retry] if we should show auth dialog
  116. * on PASSWORD_REQUIRED error.
  117. *
  118. * @returns {Promise<JitsiConnection>}
  119. */
  120. export function openConnection({id, password, retry, roomName}) {
  121. let usernameOverride
  122. = window.localStorage.getItem("xmpp_username_override");
  123. let passwordOverride
  124. = window.localStorage.getItem("xmpp_password_override");
  125. if (usernameOverride && usernameOverride.length > 0) {
  126. id = usernameOverride;
  127. }
  128. if (passwordOverride && passwordOverride.length > 0) {
  129. password = passwordOverride;
  130. }
  131. return connect(id, password, roomName).catch(function (err) {
  132. if (!retry) {
  133. throw err;
  134. }
  135. if (err === ConnectionErrors.PASSWORD_REQUIRED) {
  136. // do not retry if token is not valid
  137. if (config.token) {
  138. throw err;
  139. } else {
  140. return requestAuth();
  141. }
  142. } else {
  143. throw err;
  144. }
  145. });
  146. }