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

@ -7,12 +7,18 @@
const emit = defineEmits(['rollback']); const emit = defineEmits(['rollback']);
/**
* 定义接收的参数
*/
const props = defineProps({ const props = defineProps({
uri: { uri: {
type: String, type: String,
}, },
}); });
/**
* 定义Socket状态
*/
const state = reactive({ const state = reactive({
webSocket: ref(), // webSocket webSocket: ref(), // webSocket
lockReconnect: false, // lockReconnect: false, //
@ -27,6 +33,9 @@ const state = reactive({
}, },
}); });
/**
* 获取Token令牌
*/
const token = computed(() => { const token = computed(() => {
return useUserStore().getToken; return useUserStore().getToken;
}); });
@ -35,6 +44,9 @@ const tenant = computed(() => {
return Session.getTenant(); return Session.getTenant();
}); });
/**
* 钩子函数
*/
onMounted(() => { onMounted(() => {
initWebSocket(); initWebSocket();
}); });
@ -44,6 +56,9 @@ onUnmounted(() => {
clearTimeoutObj(state.heartbeat); clearTimeoutObj(state.heartbeat);
}); });
/**
* 初始化WebSocket对象
*/
const initWebSocket = () => { const initWebSocket = () => {
// ws // ws
let host = window.location.host; let host = window.location.host;
@ -61,11 +76,17 @@ const initWebSocket = () => {
state.webSocket.onclose = onClose; state.webSocket.onclose = onClose;
}; };
/**
* 重连机制
*/
const reconnect = () => { const reconnect = () => {
if (!token) { if (!token.value) {
return; return;
} }
if (state.lockReconnect || (state.maxReconnect !== -1 && state.reconnectTime > state.maxReconnect)) { if (
state.lockReconnect ||
(state.maxReconnect !== -1 && state.reconnectTime > state.maxReconnect)
) {
return; return;
} }
state.lockReconnect = true; state.lockReconnect = true;
@ -76,6 +97,7 @@ const reconnect = () => {
state.lockReconnect = false; state.lockReconnect = false;
}, 5000); }, 5000);
}; };
/** /**
* 清空定时器 * 清空定时器
*/ */
@ -83,6 +105,7 @@ const clearTimeoutObj = (heartbeat: any) => {
heartbeat.pingTimeoutObj && clearTimeout(heartbeat.pingTimeoutObj); heartbeat.pingTimeoutObj && clearTimeout(heartbeat.pingTimeoutObj);
heartbeat.pongTimeoutObj && clearTimeout(heartbeat.pongTimeoutObj); heartbeat.pongTimeoutObj && clearTimeout(heartbeat.pongTimeoutObj);
}; };
/** /**
* 开启心跳 * 开启心跳
*/ */
@ -116,6 +139,7 @@ const onOpen = () => {
startHeartbeat(); startHeartbeat();
state.reconnectTime = 0; state.reconnectTime = 0;
}; };
/** /**
* 连接失败事件 * 连接失败事件
* @param e * @param e
@ -133,9 +157,10 @@ const onClose = () => {
// //
reconnect(); reconnect();
}; };
/** /**
* 接收服务器推送的信息 * 接收服务器推送的信息
* @param msgEvent * @param msgEvent 消息事件
*/ */
const onMessage = (msgEvent: any) => { const onMessage = (msgEvent: any) => {
// //

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,
}, },
]; ];