ws-event.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { CloudSDKError } from '../utils/error';
  2. import { ERR_CODE } from '../config/error.config';
  3. export const CLOSE_EVENT_CODE_INFO = {
  4. 1000: {
  5. code: 1000,
  6. name: 'Normal Closure',
  7. description: 'Normal closure; the connection successfully completed whatever purpose for which it was created.'
  8. },
  9. 1001: {
  10. code: 1001,
  11. name: 'Going Away',
  12. description: 'The endpoint is going away, either because of a server failure or because the browser is navigating away from the page that opened the connection.'
  13. },
  14. 1002: {
  15. code: 1002,
  16. name: 'Protocol Error',
  17. description: 'The endpoint is terminating the connection due to a protocol error.'
  18. },
  19. 1003: {
  20. code: 1003,
  21. name: 'Unsupported Data',
  22. description: 'The connection is being terminated because the endpoint received data of a type it cannot accept (for example, a text-only endpoint received binary data).'
  23. },
  24. 1005: {
  25. code: 1005,
  26. name: 'No Status Received',
  27. description: 'Indicates that no status code was provided even though one was expected.'
  28. },
  29. 1006: {
  30. code: 1006,
  31. name: 'Abnormal Closure',
  32. description: 'Used to indicate that a connection was closed abnormally (that is, with no close frame being sent) when a status code is expected.'
  33. },
  34. 1007: {
  35. code: 1007,
  36. name: 'Invalid frame payload data',
  37. description: 'The endpoint is terminating the connection because a message was received that contained inconsistent data (e.g., non-UTF-8 data within a text message).'
  38. },
  39. 1008: {
  40. code: 1008,
  41. name: 'Policy Violation',
  42. description: 'The endpoint is terminating the connection because it received a message that violates its policy. This is a generic status code, used when codes 1003 and 1009 are not suitable.'
  43. },
  44. 1009: {
  45. code: 1009,
  46. name: 'Message too big',
  47. description: 'The endpoint is terminating the connection because a data frame was received that is too large.'
  48. },
  49. 1010: {
  50. code: 1010,
  51. name: 'Missing Extension',
  52. description: "The client is terminating the connection because it expected the server to negotiate one or more extension, but the server didn't."
  53. },
  54. 1011: {
  55. code: 1011,
  56. name: 'Internal Error',
  57. description: 'The server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling the request.'
  58. },
  59. 1012: {
  60. code: 1012,
  61. name: 'Service Restart',
  62. description: 'The server is terminating the connection because it is restarting.'
  63. },
  64. 1013: {
  65. code: 1013,
  66. name: 'Try Again Later',
  67. description: 'The server is terminating the connection due to a temporary condition, e.g. it is overloaded and is casting off some of its clients.'
  68. },
  69. 1014: {
  70. code: 1014,
  71. name: 'Bad Gateway',
  72. description: 'The server was acting as a gateway or proxy and received an invalid response from the upstream server. This is similar to 502 HTTP Status Code.'
  73. },
  74. 1015: {
  75. code: 1015,
  76. name: 'TLS Handshake',
  77. description: "Indicates that the connection was closed due to a failure to perform a TLS handshake (e.g., the server certificate can't be verified)."
  78. },
  79. 3000: {
  80. code: 3000,
  81. name: 'Reconnect WebSocket',
  82. description: 'The client is terminating the connection because it wants to reconnect'
  83. },
  84. 3001: {
  85. code: 3001,
  86. name: 'No Realtime Listeners',
  87. description: 'The client is terminating the connection because no more realtime listeners exist'
  88. },
  89. 3002: {
  90. code: 3002,
  91. name: 'Heartbeat Ping Error',
  92. description: 'The client is terminating the connection due to its failure in sending heartbeat messages'
  93. },
  94. 3003: {
  95. code: 3003,
  96. name: 'Heartbeat Pong Timeout Error',
  97. description: 'The client is terminating the connection because no heartbeat response is received from the server'
  98. },
  99. 3050: {
  100. code: 3050,
  101. name: 'Server Close',
  102. description: 'The client is terminating the connection because no heartbeat response is received from the server'
  103. }
  104. };
  105. export var CLOSE_EVENT_CODE;
  106. (function (CLOSE_EVENT_CODE) {
  107. CLOSE_EVENT_CODE[CLOSE_EVENT_CODE["NormalClosure"] = 1000] = "NormalClosure";
  108. CLOSE_EVENT_CODE[CLOSE_EVENT_CODE["GoingAway"] = 1001] = "GoingAway";
  109. CLOSE_EVENT_CODE[CLOSE_EVENT_CODE["ProtocolError"] = 1002] = "ProtocolError";
  110. CLOSE_EVENT_CODE[CLOSE_EVENT_CODE["UnsupportedData"] = 1003] = "UnsupportedData";
  111. CLOSE_EVENT_CODE[CLOSE_EVENT_CODE["NoStatusReceived"] = 1005] = "NoStatusReceived";
  112. CLOSE_EVENT_CODE[CLOSE_EVENT_CODE["AbnormalClosure"] = 1006] = "AbnormalClosure";
  113. CLOSE_EVENT_CODE[CLOSE_EVENT_CODE["InvalidFramePayloadData"] = 1007] = "InvalidFramePayloadData";
  114. CLOSE_EVENT_CODE[CLOSE_EVENT_CODE["PolicyViolation"] = 1008] = "PolicyViolation";
  115. CLOSE_EVENT_CODE[CLOSE_EVENT_CODE["MessageTooBig"] = 1009] = "MessageTooBig";
  116. CLOSE_EVENT_CODE[CLOSE_EVENT_CODE["MissingExtension"] = 1010] = "MissingExtension";
  117. CLOSE_EVENT_CODE[CLOSE_EVENT_CODE["InternalError"] = 1011] = "InternalError";
  118. CLOSE_EVENT_CODE[CLOSE_EVENT_CODE["ServiceRestart"] = 1012] = "ServiceRestart";
  119. CLOSE_EVENT_CODE[CLOSE_EVENT_CODE["TryAgainLater"] = 1013] = "TryAgainLater";
  120. CLOSE_EVENT_CODE[CLOSE_EVENT_CODE["BadGateway"] = 1014] = "BadGateway";
  121. CLOSE_EVENT_CODE[CLOSE_EVENT_CODE["TLSHandshake"] = 1015] = "TLSHandshake";
  122. CLOSE_EVENT_CODE[CLOSE_EVENT_CODE["ReconnectWebSocket"] = 3000] = "ReconnectWebSocket";
  123. CLOSE_EVENT_CODE[CLOSE_EVENT_CODE["NoRealtimeListeners"] = 3001] = "NoRealtimeListeners";
  124. CLOSE_EVENT_CODE[CLOSE_EVENT_CODE["HeartbeatPingError"] = 3002] = "HeartbeatPingError";
  125. CLOSE_EVENT_CODE[CLOSE_EVENT_CODE["HeartbeatPongTimeoutError"] = 3003] = "HeartbeatPongTimeoutError";
  126. CLOSE_EVENT_CODE[CLOSE_EVENT_CODE["NoAuthentication"] = 3050] = "NoAuthentication";
  127. })(CLOSE_EVENT_CODE || (CLOSE_EVENT_CODE = {}));
  128. export const getWSCloseError = (code, reason) => {
  129. const info = CLOSE_EVENT_CODE_INFO[code];
  130. const errMsg = !info
  131. ? `code ${code}`
  132. : `${info.name}, code ${code}, reason ${reason || info.description}`;
  133. return new CloudSDKError({
  134. errCode: ERR_CODE.SDK_DATABASE_REALTIME_LISTENER_WEBSOCKET_CONNECTION_CLOSED,
  135. errMsg
  136. });
  137. };