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

strophe.util.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* global __filename, Strophe */
  2. /**
  3. * Strophe logger implementation. Logs from level WARN and above.
  4. */
  5. import { getLogger } from 'jitsi-meet-logger';
  6. const logger = getLogger(__filename);
  7. import GlobalOnErrorHandler from '../util/GlobalOnErrorHandler';
  8. /**
  9. * This is the last HTTP error status captured from Strophe debug logs.
  10. * The purpose of storing it is to distinguish between the network and
  11. * infrastructure reason for connection being dropped (see connectionHandler in
  12. * xmpp.js). The value will be cleared (-1) if the subsequent request succeeds
  13. * which means that the failure could be transient.
  14. *
  15. * FIXME in the latest Strophe (not released on npm) there is API to handle
  16. * particular HTTP errors, but there is no way to learn if the subsequent
  17. * request succeeded in order to tell if the error was one time incident or if
  18. * it was the reason for dropping the connection by Strophe (the connection is
  19. * dropped after 5 subsequent failures). Ideally Strophe should provide more
  20. * details about the reason on why the connection stopped.
  21. *
  22. * @type {number}
  23. */
  24. let lastErrorStatus = -1;
  25. /**
  26. * A regular expression used to catch Strophe's log message indicating that the
  27. * last BOSH request was successful. When there is such message seen the
  28. * {@link lastErrorStatus} will be set back to '-1'.
  29. * @type {RegExp}
  30. */
  31. const resetLastErrorStatusRegExpr = /request id \d+.\d+ got 200/;
  32. /**
  33. * A regular expression used to capture the current value of the BOSH request
  34. * error status (HTTP error code or '0' or something else).
  35. * @type {RegExp}
  36. */
  37. const lastErrorStatusRegExpr
  38. = /request errored, status: (\d+), number of errors: \d+/;
  39. export default function() {
  40. Strophe.log = function(level, msg) {
  41. // Our global handler reports uncaught errors to the stats which may
  42. // interpret those as partial call failure.
  43. // Strophe log entry about secondary request timeout does not mean that
  44. // it's a final failure(the request will be restarted), so we lower it's
  45. // level here to a warning.
  46. logger.trace('Strophe', level, msg);
  47. if (typeof msg === 'string'
  48. && msg.indexOf('Request ') !== -1
  49. && msg.indexOf('timed out (secondary), restarting') !== -1) {
  50. // eslint-disable-next-line no-param-reassign
  51. level = Strophe.LogLevel.WARN;
  52. }
  53. /* eslint-disable no-case-declarations */
  54. switch (level) {
  55. case Strophe.LogLevel.DEBUG:
  56. // The log message which reports successful status is logged
  57. // on Strophe's DEBUG level
  58. if (lastErrorStatus !== -1
  59. && resetLastErrorStatusRegExpr.test(msg)) {
  60. logger.debug('Reset lastErrorStatus');
  61. lastErrorStatus = -1;
  62. }
  63. break;
  64. case Strophe.LogLevel.WARN:
  65. logger.warn(`Strophe: ${msg}`);
  66. const errStatusCapture = lastErrorStatusRegExpr.exec(msg);
  67. if (errStatusCapture && errStatusCapture.length === 2) {
  68. lastErrorStatus = parseInt(errStatusCapture[1], 10);
  69. logger.debug(`lastErrorStatus set to: ${lastErrorStatus}`);
  70. }
  71. break;
  72. case Strophe.LogLevel.ERROR:
  73. case Strophe.LogLevel.FATAL:
  74. // eslint-disable-next-line no-param-reassign
  75. msg = `Strophe: ${msg}`;
  76. GlobalOnErrorHandler.callErrorHandler(new Error(msg));
  77. logger.error(msg);
  78. break;
  79. }
  80. /* eslint-enable no-case-declarations */
  81. };
  82. /**
  83. * Returns error status (HTTP error code) of the last BOSH request.
  84. *
  85. * @return {number} HTTP error code, '0' for unknown or "god knows what"
  86. * (this is a hack).
  87. */
  88. Strophe.getLastErrorStatus = function() {
  89. return lastErrorStatus;
  90. };
  91. Strophe.getStatusString = function(status) {
  92. switch (status) {
  93. case Strophe.Status.ERROR:
  94. return 'ERROR';
  95. case Strophe.Status.CONNECTING:
  96. return 'CONNECTING';
  97. case Strophe.Status.CONNFAIL:
  98. return 'CONNFAIL';
  99. case Strophe.Status.AUTHENTICATING:
  100. return 'AUTHENTICATING';
  101. case Strophe.Status.AUTHFAIL:
  102. return 'AUTHFAIL';
  103. case Strophe.Status.CONNECTED:
  104. return 'CONNECTED';
  105. case Strophe.Status.DISCONNECTED:
  106. return 'DISCONNECTED';
  107. case Strophe.Status.DISCONNECTING:
  108. return 'DISCONNECTING';
  109. case Strophe.Status.ATTACHED:
  110. return 'ATTACHED';
  111. default:
  112. return 'unknown';
  113. }
  114. };
  115. }