Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

strophe.util.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. level = Strophe.LogLevel.WARN;
  51. }
  52. /* eslint-disable no-case-declarations */
  53. switch (level) {
  54. case Strophe.LogLevel.DEBUG:
  55. if (resetLastErrorStatusRegExpr.test(msg)) {
  56. logger.debug("Reset lastErrorStatus");
  57. lastErrorStatus = -1;
  58. }
  59. break;
  60. case Strophe.LogLevel.WARN:
  61. logger.warn("Strophe: " + msg);
  62. const errStatusCapture = lastErrorStatusRegExpr.exec(msg);
  63. if (errStatusCapture && errStatusCapture.length === 2) {
  64. lastErrorStatus = parseInt(errStatusCapture[1]);
  65. logger.debug(
  66. "lastErrorStatus set to: " + lastErrorStatus);
  67. }
  68. break;
  69. case Strophe.LogLevel.ERROR:
  70. case Strophe.LogLevel.FATAL:
  71. msg = "Strophe: " + msg;
  72. GlobalOnErrorHandler.callErrorHandler(new Error(msg));
  73. logger.error(msg);
  74. break;
  75. }
  76. /* eslint-enable no-case-declarations */
  77. };
  78. /**
  79. * Returns error status (HTTP error code) of the last BOSH request.
  80. *
  81. * @return {number} HTTP error code, '0' for unknown or "god knows what"
  82. * (this is a hack).
  83. */
  84. Strophe.getLastErrorStatus = function () {
  85. return lastErrorStatus;
  86. };
  87. Strophe.getStatusString = function (status) {
  88. switch (status) {
  89. case Strophe.Status.ERROR:
  90. return "ERROR";
  91. case Strophe.Status.CONNECTING:
  92. return "CONNECTING";
  93. case Strophe.Status.CONNFAIL:
  94. return "CONNFAIL";
  95. case Strophe.Status.AUTHENTICATING:
  96. return "AUTHENTICATING";
  97. case Strophe.Status.AUTHFAIL:
  98. return "AUTHFAIL";
  99. case Strophe.Status.CONNECTED:
  100. return "CONNECTED";
  101. case Strophe.Status.DISCONNECTED:
  102. return "DISCONNECTED";
  103. case Strophe.Status.DISCONNECTING:
  104. return "DISCONNECTING";
  105. case Strophe.Status.ATTACHED:
  106. return "ATTACHED";
  107. default:
  108. return "unknown";
  109. }
  110. };
  111. }