From ae16757984e93aeebbc8b08d3ab1fdbc41642eca Mon Sep 17 00:00:00 2001 From: wangys <3401275564@qq.com> Date: Mon, 22 Dec 2025 17:08:49 +0800 Subject: [PATCH] =?UTF-8?q?ws=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/views/public/ChatRoom/ChatRoomView.vue | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/urbanLifelineWeb/packages/workcase/src/views/public/ChatRoom/ChatRoomView.vue b/urbanLifelineWeb/packages/workcase/src/views/public/ChatRoom/ChatRoomView.vue index 8f25a3e6..54f65957 100644 --- a/urbanLifelineWeb/packages/workcase/src/views/public/ChatRoom/ChatRoomView.vue +++ b/urbanLifelineWeb/packages/workcase/src/views/public/ChatRoom/ChatRoomView.vue @@ -126,12 +126,13 @@ import type { ChatRoomVO, ChatRoomMessageVO, TbChatRoomMessageDTO } from 'shared import SockJS from 'sockjs-client' import { Client } from '@stomp/stompjs' -// WebSocket配置 (通过网关代理访问workcase服务) -// 原生WebSocket URL (ws://或wss://) +// WebSocket配置 (通过Nginx代理访问网关,再到workcase服务) +// SockJS URL (http://) const getWsUrl = () => { const token = localStorage.getItem('token') || '' - // 直接连接网关,跳过Nginx调试 - return `ws://localhost:8180/urban-lifeline/workcase/ws/chat?token=${encodeURIComponent(token)}` + const protocol = window.location.protocol + const host = window.location.host + return `${protocol}//${host}/api/urban-lifeline/workcase/ws/chat-sockjs?token=${encodeURIComponent(token)}` } // STOMP客户端 @@ -304,16 +305,16 @@ const formatTime = (time: string | null | undefined) => { // ==================== WebSocket连接管理 ==================== -// 初始化WebSocket连接(使用原生WebSocket,不降级) +// 初始化WebSocket连接(支持SockJS降级) const initWebSocket = () => { const token = localStorage.getItem('token') || '' const wsUrl = getWsUrl() console.log('WebSocket连接URL:', wsUrl) - // 创建STOMP客户端,使用原生WebSocket + // 创建STOMP客户端,使用SockJS(支持降级) stompClient = new Client({ - brokerURL: wsUrl, // 使用原生WebSocket URL + webSocketFactory: () => new SockJS(wsUrl), connectHeaders: { Authorization: `Bearer ${token}` },