ws修正
This commit is contained in:
@@ -126,12 +126,13 @@ import type { ChatRoomVO, ChatRoomMessageVO, TbChatRoomMessageDTO } from 'shared
|
|||||||
import SockJS from 'sockjs-client'
|
import SockJS from 'sockjs-client'
|
||||||
import { Client } from '@stomp/stompjs'
|
import { Client } from '@stomp/stompjs'
|
||||||
|
|
||||||
// WebSocket配置 (通过网关代理访问workcase服务)
|
// WebSocket配置 (通过Nginx代理访问网关,再到workcase服务)
|
||||||
// 原生WebSocket URL (ws://或wss://)
|
// SockJS URL (http://)
|
||||||
const getWsUrl = () => {
|
const getWsUrl = () => {
|
||||||
const token = localStorage.getItem('token') || ''
|
const token = localStorage.getItem('token') || ''
|
||||||
// 直接连接网关,跳过Nginx调试
|
const protocol = window.location.protocol
|
||||||
return `ws://localhost:8180/urban-lifeline/workcase/ws/chat?token=${encodeURIComponent(token)}`
|
const host = window.location.host
|
||||||
|
return `${protocol}//${host}/api/urban-lifeline/workcase/ws/chat-sockjs?token=${encodeURIComponent(token)}`
|
||||||
}
|
}
|
||||||
|
|
||||||
// STOMP客户端
|
// STOMP客户端
|
||||||
@@ -304,16 +305,16 @@ const formatTime = (time: string | null | undefined) => {
|
|||||||
|
|
||||||
// ==================== WebSocket连接管理 ====================
|
// ==================== WebSocket连接管理 ====================
|
||||||
|
|
||||||
// 初始化WebSocket连接(使用原生WebSocket,不降级)
|
// 初始化WebSocket连接(支持SockJS降级)
|
||||||
const initWebSocket = () => {
|
const initWebSocket = () => {
|
||||||
const token = localStorage.getItem('token') || ''
|
const token = localStorage.getItem('token') || ''
|
||||||
const wsUrl = getWsUrl()
|
const wsUrl = getWsUrl()
|
||||||
|
|
||||||
console.log('WebSocket连接URL:', wsUrl)
|
console.log('WebSocket连接URL:', wsUrl)
|
||||||
|
|
||||||
// 创建STOMP客户端,使用原生WebSocket
|
// 创建STOMP客户端,使用SockJS(支持降级)
|
||||||
stompClient = new Client({
|
stompClient = new Client({
|
||||||
brokerURL: wsUrl, // 使用原生WebSocket URL
|
webSocketFactory: () => new SockJS(wsUrl),
|
||||||
connectHeaders: {
|
connectHeaders: {
|
||||||
Authorization: `Bearer ${token}`
|
Authorization: `Bearer ${token}`
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user