useWebSocket.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { onBeforeUnmount, reactive, ref } from 'vue';
  2. import { baseUrl, websocketPath } from '@/sheep/config';
  3. import { copyValueToTarget } from '@/sheep/util';
  4. import { getRefreshToken } from '@/sheep/request';
  5. /**
  6. * WebSocket 创建 hook
  7. * @param opt 连接配置
  8. * @return {{options: *}}
  9. */
  10. export function useWebSocket(opt) {
  11. // ws://42.194.163.46:9095/infra/ws?token=accf7d65f3734cd2b92caf4956363be8
  12. const options = reactive({
  13. url: (baseUrl + websocketPath).replace('http', 'ws') + '?token=' + getRefreshToken(), // ws 地址
  14. isReconnecting: false, // 正在重新连接
  15. reconnectInterval: 3000, // 重连间隔,单位毫秒
  16. heartBeatInterval: 5000, // 心跳间隔,单位毫秒
  17. pingTimeoutDuration: 1000, // 超过这个时间,后端没有返回pong,则判定后端断线了。
  18. heartBeatTimer: null, // 心跳计时器
  19. destroy: false, // 是否销毁
  20. pingTimeout: null, // 心跳检测定时器
  21. reconnectTimeout: null, // 重连定时器ID的属性
  22. onConnected: () => {}, // 连接成功时触发
  23. onClosed: () => {}, // 连接关闭时触发
  24. onMessage: (data) => {}, // 收到消息
  25. });
  26. const SocketTask = ref(null); // SocketTask 由 uni.connectSocket() 接口创建
  27. const initEventListeners = () => {
  28. // 监听 WebSocket 连接打开事件
  29. SocketTask.value.onOpen(() => {
  30. console.log('WebSocket 连接成功');
  31. // 连接成功时触发
  32. options.onConnected();
  33. // 开启心跳检查
  34. startHeartBeat();
  35. });
  36. // 监听 WebSocket 接受到服务器的消息事件
  37. SocketTask.value.onMessage((res) => {
  38. try {
  39. if (res.data === 'pong') {
  40. // 收到心跳重置心跳超时检查
  41. resetPingTimeout();
  42. } else {
  43. options.onMessage(JSON.parse(res.data));
  44. }
  45. } catch (error) {
  46. console.error(error);
  47. }
  48. });
  49. // 监听 WebSocket 连接关闭事件
  50. SocketTask.value.onClose((event) => {
  51. // 情况一:实例销毁
  52. if (options.destroy) {
  53. options.onClosed();
  54. } else {
  55. // 情况二:连接失败重连
  56. // 停止心跳检查
  57. stopHeartBeat();
  58. // 重连
  59. reconnect();
  60. }
  61. });
  62. };
  63. // 发送消息
  64. const sendMessage = (message) => {
  65. if (SocketTask.value && !options.destroy) {
  66. SocketTask.value.send({ data: message });
  67. }
  68. };
  69. // 开始心跳检查
  70. const startHeartBeat = () => {
  71. options.heartBeatTimer = setInterval(() => {
  72. sendMessage('ping');
  73. options.pingTimeout = setTimeout(() => {
  74. // 如果在超时时间内没有收到 pong,则认为连接断开
  75. reconnect();
  76. }, options.pingTimeoutDuration);
  77. }, options.heartBeatInterval);
  78. };
  79. // 停止心跳检查
  80. const stopHeartBeat = () => {
  81. clearInterval(options.heartBeatTimer);
  82. resetPingTimeout();
  83. };
  84. // WebSocket 重连
  85. const reconnect = () => {
  86. if (options.destroy || !SocketTask.value) {
  87. // 如果WebSocket已被销毁或尚未完全关闭,不进行重连
  88. return;
  89. }
  90. // 重连中
  91. options.isReconnecting = true;
  92. // 清除现有的重连标志,以避免多次重连
  93. if (options.reconnectTimeout) {
  94. clearTimeout(options.reconnectTimeout);
  95. }
  96. // 设置重连延迟
  97. options.reconnectTimeout = setTimeout(() => {
  98. // 检查组件是否仍在运行和WebSocket是否关闭
  99. if (!options.destroy) {
  100. // 重置重连标志
  101. options.isReconnecting = false;
  102. // 初始化新的WebSocket连接
  103. initSocket();
  104. }
  105. }, options.reconnectInterval);
  106. };
  107. const resetPingTimeout = () => {
  108. if (options.pingTimeout) {
  109. clearTimeout(options.pingTimeout);
  110. options.pingTimeout = null; // 清除超时ID
  111. }
  112. };
  113. const close = () => {
  114. options.destroy = true;
  115. stopHeartBeat();
  116. if (options.reconnectTimeout) {
  117. clearTimeout(options.reconnectTimeout);
  118. }
  119. if (SocketTask.value) {
  120. SocketTask.value.close();
  121. SocketTask.value = null;
  122. }
  123. };
  124. const initSocket = () => {
  125. options.destroy = false;
  126. copyValueToTarget(options, opt);
  127. SocketTask.value = uni.connectSocket({
  128. url: options.url,
  129. complete: () => {},
  130. success: () => {},
  131. });
  132. initEventListeners();
  133. };
  134. initSocket();
  135. onBeforeUnmount(() => {
  136. close();
  137. });
  138. return { options };
  139. }