useVentWebSocket.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { reactive, watch, unref, computed, ref, Ref, watchEffect, WatchStopHandle } from 'vue';
  2. import { useWebSocket as $useWebSocket } from '@vueuse/core';
  3. import { useGlobSetting } from '/@/hooks/setting';
  4. import { useUserStore } from '/@/store/modules/user';
  5. import { useVentStore } from '/@/store/modules/vent';
  6. import { getToken } from '/@/utils/auth';
  7. const glob = useGlobSetting();
  8. const userStore = useUserStore();
  9. const state = reactive({
  10. server:
  11. glob.domainUrl?.replace('https://', 'wss://').replace('http://', 'ws://') +
  12. '/deviceSocket/' +
  13. unref(userStore.getUserInfo).username +
  14. '?token=' +
  15. getToken(),
  16. sendValue: '',
  17. recordList: [] as { id: number; time: number; res: string }[],
  18. });
  19. const ventStore = useVentStore();
  20. const resultWebSocket = computed(() => {
  21. return ventStore.getResultWebSocket;
  22. });
  23. const data = computed(() => (resultWebSocket.value ? resultWebSocket.value.data : ''));
  24. const status = computed(() => (resultWebSocket.value ? resultWebSocket.value.status : ''));
  25. const stop = <Ref<WatchStopHandle>>ref();
  26. const startWatchEffect = () => {
  27. stop.value = watchEffect(() => {
  28. if (status.value === 'OPEN' && !data.value) {
  29. try {
  30. resultWebSocket.value.send(state.sendValue);
  31. console.log(222, 'sendValue', state.sendValue, status.value);
  32. } catch (e) {
  33. console.error(e);
  34. }
  35. }
  36. if (data.value && status.value === 'OPEN') {
  37. try {
  38. const res = JSON.parse(data.value);
  39. if (res.cmd === 'monitordata') {
  40. state.recordList = [];
  41. const list: [] = res.msgTxt[0].datalist;
  42. list.forEach((item: any) => {
  43. const resultItem = { ...item };
  44. const readData = item.readData;
  45. for (const key in readData) {
  46. resultItem[key] = readData[key];
  47. }
  48. state.recordList.push(resultItem);
  49. });
  50. }
  51. } catch (error) {
  52. state.recordList.push({
  53. res: data.value,
  54. id: Math.ceil(Math.random() * 1000),
  55. time: new Date().getTime(),
  56. });
  57. }
  58. }
  59. });
  60. };
  61. function connectWebSocket() {
  62. if (!resultWebSocket.value) {
  63. console.log('测试链接。。。。');
  64. const result = $useWebSocket(state.server, {
  65. // 自动重连
  66. autoReconnect: false,
  67. // 心跳检测
  68. heartbeat: true,
  69. //protocols: [token],
  70. });
  71. ventStore.setResultWebSocket(result);
  72. startWatchEffect();
  73. }
  74. }
  75. export function initWebSocket(sendValue) {
  76. if (resultWebSocket.value) {
  77. closeWebSocket();
  78. }
  79. state.sendValue = sendValue;
  80. connectWebSocket();
  81. }
  82. export function getRecordList() {
  83. return state.recordList;
  84. }
  85. export function closeWebSocket() {
  86. state.recordList = [];
  87. ventStore.setResultWebSocket(null);
  88. }