Files
vue3-element-admin/src/plugins/websocket.ts

40 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useDictSync } from "@/hooks/websocket/services/useDictSync";
import { getAccessToken } from "@/utils/auth";
/**
* 初始化WebSocket服务
*/
export function setupWebSocket() {
console.log("[WebSocketPlugin] 开始初始化WebSocket服务...");
// 检查token是否存在
const token = getAccessToken();
if (!token) {
console.warn(
"[WebSocketPlugin] 未找到访问令牌WebSocket初始化已跳过。用户登录后将自动重新连接。"
);
return;
}
try {
// 延迟初始化,确保应用完全启动
setTimeout(() => {
const dictWebSocket = useDictSync();
// 初始化字典WebSocket服务
dictWebSocket.initWebSocket();
console.log("[WebSocketPlugin] 字典WebSocket初始化完成");
// 在窗口关闭前断开WebSocket连接
window.addEventListener("beforeunload", () => {
console.log("[WebSocketPlugin] 窗口即将关闭断开WebSocket连接");
dictWebSocket.closeWebSocket();
});
console.log("[WebSocketPlugin] WebSocket服务初始化完成");
}, 1000); // 延迟1秒初始化
} catch (error) {
console.error("[WebSocketPlugin] 初始化WebSocket服务失败:", error);
}
}