wsClientData.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { randomUUID } from "crypto";
  2. import Logs from "./logs.js";
  3. /**
  4. * 解析推送过来的消息
  5. * @param {*} data
  6. * @returns {object}
  7. */
  8. const parseMessage = (data) => {
  9. let message;
  10. try {
  11. message = JSON.parse(data);
  12. }
  13. catch(e) {
  14. message = { text: data };
  15. }
  16. return message;
  17. }
  18. class WebSocketClient {
  19. #wsClient;
  20. constructor(wsClient) {
  21. this.#wsClient = wsClient;
  22. }
  23. on(type, callback) {
  24. if (type == 'message') {
  25. this.#wsClient.on('message', (event) => {
  26. const data = parseMessage(event.data);
  27. callback(data);
  28. });
  29. }
  30. else {
  31. this.#wsClient.on(type, callback);
  32. }
  33. }
  34. send(message) {
  35. this.#wsClient.send(JSON.stringify(message));
  36. }
  37. close() {
  38. this.#wsClient.close();
  39. }
  40. }
  41. class WsClientData {
  42. #wsClient;
  43. #name;
  44. #request = {
  45. callbacks: {},
  46. functions: {}
  47. };
  48. #response = {
  49. functions: {}
  50. };
  51. constructor(wsClient, name='wsClient') {
  52. this.#wsClient = wsClient.constructor.name === 'WebSocket' ? new WebSocketClient(wsClient) : wsClient;
  53. this.#name = name;
  54. this.#wsClient.on('message', (message) => {
  55. this.#handleMessage(message);
  56. });
  57. this.#wsClient.on('error', (error) => {
  58. Logs.err('wsClient error', error);
  59. });
  60. this.#wsClient.on('close', () => {
  61. Logs.outDev('wsClient closed');
  62. });
  63. }
  64. get(type, params) {
  65. return new Promise((resolve, reject) => {
  66. const id = randomUUID();
  67. try {
  68. this.#wsClient.send({ method: 'get', type, id, params });
  69. this.#request.callbacks[id] = resolve;
  70. }
  71. catch (error) {
  72. delete this.#request.callbacks[id];
  73. reject(error);
  74. }
  75. });
  76. }
  77. registerResponse(type, func) {
  78. this.#response.functions[type] = func;
  79. }
  80. #responseData(type, params, id) {
  81. const func = this.#response.functions[type];
  82. func?.(params).then(data => {
  83. this.#wsClient.send({ type: 'response', data, id });
  84. });
  85. }
  86. post(type, data) {
  87. this.#wsClient.send({ method: 'post', type, data });
  88. }
  89. registerRequest(type, func) {
  90. this.#request.functions[type] = func;
  91. }
  92. #requestData(type, data) {
  93. const func = this.#request.functions[type];
  94. func?.(data);
  95. }
  96. #handleMessage(message) {
  97. const { callbacks } = this.#request;
  98. const { method, type, data, error, id, params } = message;
  99. if (error) {
  100. console.error(error);
  101. }
  102. if (method == 'get' && id) {
  103. this.#responseData(type, params, id);
  104. }
  105. else if (type == 'response' && id && callbacks[id]) {
  106. callbacks[id](data);
  107. delete callbacks[id];
  108. }
  109. else if (method == 'post' && type) {
  110. this.#requestData(type, data);
  111. }
  112. }
  113. }
  114. export default WsClientData;