This commit is contained in:
陈红丽 2024-12-18 15:55:44 +08:00
commit e6c1599cb2
3 changed files with 164 additions and 144 deletions

View File

@ -1,155 +1,180 @@
<template> <template>
<div></div> <div></div>
</template> </template>
<script setup lang="ts" name="global-websocket"> <script setup lang="ts" name="global-websocket">
import { reactive, ref, computed,onMounted,onUnmounted } from 'vue'; import { reactive, ref, computed, onMounted, onUnmounted } from 'vue';
import { useUserStore } from '@/store/modules/user'; import { useUserStore } from '@/store/modules/user';
const emit = defineEmits(['rollback']); const emit = defineEmits(['rollback']);
const props = defineProps({ /**
uri: { * 定义接收的参数
type: String, */
}, const props = defineProps({
}); uri: {
type: String,
},
});
const state = reactive({ /**
webSocket: ref(), // webSocket * 定义Socket状态
lockReconnect: false, // */
maxReconnect: 6, // -1 const state = reactive({
reconnectTime: 0, // webSocket: ref(), // webSocket
heartbeat: { lockReconnect: false, //
interval: 30 * 1000, // maxReconnect: 6, // -1
timeout: 10 * 1000, // reconnectTime: 0, //
pingTimeoutObj: ref(), // heartbeat: {
pongTimeoutObj: ref(), // interval: 30 * 1000, //
pingMessage: JSON.stringify({ type: 'ping' }), // timeout: 10 * 1000, //
}, pingTimeoutObj: ref(), //
}); pongTimeoutObj: ref(), //
pingMessage: JSON.stringify({ type: 'ping' }), //
},
});
const token = computed(() => { /**
return useUserStore().getToken; * 获取Token令牌
}); */
const token = computed(() => {
return useUserStore().getToken;
});
const tenant = computed(() => { const tenant = computed(() => {
return Session.getTenant(); return Session.getTenant();
}); });
onMounted(() => { /**
initWebSocket(); * 钩子函数
}); */
onMounted(() => {
initWebSocket();
});
onUnmounted(() => { onUnmounted(() => {
state.webSocket.close(); state.webSocket.close();
clearTimeoutObj(state.heartbeat); clearTimeoutObj(state.heartbeat);
}); });
const initWebSocket = () => { /**
// ws * 初始化WebSocket对象
let host = window.location.host; */
let wsUri =`${location.protocol === 'https:' ? 'wss' : 'ws'}://${host}${props.uri}`; const initWebSocket = () => {
// ws
let host = window.location.host;
let wsUri = `${location.protocol === 'https:' ? 'wss' : 'ws'}://${host}${props.uri}`;
// //
state.webSocket = new WebSocket(wsUri); state.webSocket = new WebSocket(wsUri);
// //
state.webSocket.onopen = onOpen; state.webSocket.onopen = onOpen;
// //
state.webSocket.onerror = onError; state.webSocket.onerror = onError;
// //
state.webSocket.onmessage = onMessage; state.webSocket.onmessage = onMessage;
// //
state.webSocket.onclose = onClose; state.webSocket.onclose = onClose;
}; };
const reconnect = () => { /**
if (!token) { * 重连机制
return; */
} const reconnect = () => {
if (state.lockReconnect || (state.maxReconnect !== -1 && state.reconnectTime > state.maxReconnect)) { if (!token.value) {
return; return;
} }
state.lockReconnect = true; if (
setTimeout(() => { state.lockReconnect ||
state.reconnectTime++; (state.maxReconnect !== -1 && state.reconnectTime > state.maxReconnect)
// ) {
initWebSocket(); return;
state.lockReconnect = false; }
}, 5000); state.lockReconnect = true;
}; setTimeout(() => {
/** state.reconnectTime++;
* 清空定时器 //
*/ initWebSocket();
const clearTimeoutObj = (heartbeat: any) => { state.lockReconnect = false;
heartbeat.pingTimeoutObj && clearTimeout(heartbeat.pingTimeoutObj); }, 5000);
heartbeat.pongTimeoutObj && clearTimeout(heartbeat.pongTimeoutObj); };
};
/**
* 开启心跳
*/
const startHeartbeat = () => {
const webSocket = state.webSocket;
const heartbeat = state.heartbeat;
//
clearTimeoutObj(heartbeat);
//
heartbeat.pingTimeoutObj = setTimeout(() => {
//
if (webSocket.readyState === 1) {
//
webSocket.send(heartbeat.pingMessage);
//
heartbeat.pongTimeoutObj = setTimeout(() => {
webSocket.close();
}, heartbeat.timeout);
} else {
//
reconnect();
}
}, heartbeat.interval);
};
/** /**
* 连接成功事件 * 清空定时器
*/ */
const onOpen = () => { const clearTimeoutObj = (heartbeat: any) => {
// heartbeat.pingTimeoutObj && clearTimeout(heartbeat.pingTimeoutObj);
startHeartbeat(); heartbeat.pongTimeoutObj && clearTimeout(heartbeat.pongTimeoutObj);
state.reconnectTime = 0; };
};
/**
* 连接失败事件
* @param e
*/
const onError = () => {
//
reconnect();
};
/** /**
* 连接关闭事件 * 开启心跳
* @param e */
*/ const startHeartbeat = () => {
const onClose = () => { const webSocket = state.webSocket;
// const heartbeat = state.heartbeat;
reconnect(); //
}; clearTimeoutObj(heartbeat);
/** //
* 接收服务器推送的信息 heartbeat.pingTimeoutObj = setTimeout(() => {
* @param msgEvent //
*/ if (webSocket.readyState === 1) {
const onMessage = (msgEvent: any) => { //
// webSocket.send(heartbeat.pingMessage);
startHeartbeat(); //
// if (msgEvent.data.indexOf('pong') > 0) { heartbeat.pongTimeoutObj = setTimeout(() => {
// return; webSocket.close();
// } }, heartbeat.timeout);
// let text ='' } else {
// try{ //
// text = JSON.parse(msgEvent.data); reconnect();
// }catch(e){ }
// return }, heartbeat.interval);
// } };
emit('rollback', msgEvent.data); /**
}; * 连接成功事件
*/
const onOpen = () => {
//
startHeartbeat();
state.reconnectTime = 0;
};
/**
* 连接失败事件
* @param e
*/
const onError = () => {
//
reconnect();
};
/**
* 连接关闭事件
* @param e
*/
const onClose = () => {
//
reconnect();
};
/**
* 接收服务器推送的信息
* @param msgEvent 消息事件
*/
const onMessage = (msgEvent: any) => {
//
startHeartbeat();
// if (msgEvent.data.indexOf('pong') > 0) {
// return;
// }
// let text =''
// try{
// text = JSON.parse(msgEvent.data);
// }catch(e){
// return
// }
emit('rollback', msgEvent.data);
};
</script> </script>

View File

@ -14,11 +14,6 @@ export const columns = [
{ {
title: '配置编码', title: '配置编码',
key: 'code', key: 'code',
width: 100, width: 200,
},
{
title: '排序',
key: 'sort',
width: 100,
}, },
]; ];

View File

@ -14,6 +14,6 @@ export const columns = [
{ {
title: '字典编码', title: '字典编码',
key: 'code', key: 'code',
width: 100, width: 200,
}, },
]; ];