fix(websocket): 🐛 完善WebSocket端点配置检查逻辑

在所有WebSocket相关组件中添加对VITE_APP_WS_ENDPOINT的检查
确保未配置WebSocket端点时不会尝试建立连接
防止出现"brokerURL or webSocketFactory must be provided"错误
This commit is contained in:
Ray.Hao
2025-04-29 23:46:32 +08:00
parent 142d808a5e
commit d619e3c2e6
3 changed files with 26 additions and 0 deletions

View File

@@ -59,6 +59,12 @@ export function useStomp(options: UseStompOptions = {}) {
return;
}
// 检查WebSocket端点是否配置
if (!brokerURL.value) {
console.error("WebSocket连接失败: 未配置WebSocket端点URL");
return;
}
// 每次连接前重新获取最新令牌不依赖之前的token值
const currentToken = getAccessToken();
@@ -190,6 +196,12 @@ export function useStomp(options: UseStompOptions = {}) {
* 激活连接(如果已经连接或正在激活则直接返回)
*/
const connect = () => {
// 检查是否有配置WebSocket端点
if (!brokerURL.value) {
console.error("WebSocket连接失败: 未配置WebSocket端点URL");
return;
}
if (!client.value) {
initializeClient();
}

View File

@@ -59,6 +59,13 @@ function createDictSyncHook() {
*/
const initWebSocket = async () => {
try {
// 检查是否配置了WebSocket端点
const wsEndpoint = import.meta.env.VITE_APP_WS_ENDPOINT;
if (!wsEndpoint) {
console.log("[WebSocket] 未配置WebSocket端点,跳过连接");
return;
}
// 连接WebSocket
connect();

View File

@@ -91,6 +91,13 @@ export function useOnlineCount() {
const initWebSocket = () => {
if (isConnecting.value) return;
// 检查WebSocket端点是否配置
const wsEndpoint = import.meta.env.VITE_APP_WS_ENDPOINT;
if (!wsEndpoint) {
console.log("未配置WebSocket端点(VITE_APP_WS_ENDPOINT),跳过WebSocket连接");
return;
}
// 检查是否有可用的令牌
const hasToken = !!getAccessToken();
if (!hasToken) {