From 45339902aaa13806bcababff946b8e93b681bf0b Mon Sep 17 00:00:00 2001 From: zzlgreat Date: Wed, 10 Dec 2025 11:43:56 +0800 Subject: [PATCH] update pay ui --- .../components/FlexScreen/hooks/constants.ts | 37 +++++++++++++++++-- .../FlexScreen/hooks/useRealtimeQuote.ts | 13 +++++-- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/views/StockOverview/components/FlexScreen/hooks/constants.ts b/src/views/StockOverview/components/FlexScreen/hooks/constants.ts index 6f1717b3..f9d41b30 100644 --- a/src/views/StockOverview/components/FlexScreen/hooks/constants.ts +++ b/src/views/StockOverview/components/FlexScreen/hooks/constants.ts @@ -4,12 +4,41 @@ import type { Exchange } from '../types'; -/** WebSocket 服务地址 */ -export const WS_CONFIG: Record = { - SSE: 'ws://49.232.185.254:8765', // 上交所 - SZSE: 'ws://222.128.1.157:8765', // 深交所 +/** + * 获取 WebSocket 配置 + * - 生产环境 (HTTPS): 通过 Nginx 代理使用 wss:// + * - 开发环境 (HTTP): 直连 ws:// + */ +const getWsConfig = (): Record => { + // 服务端渲染或测试环境使用默认配置 + if (typeof window === 'undefined') { + return { + SSE: 'ws://49.232.185.254:8765', + SZSE: 'ws://222.128.1.157:8765', + }; + } + + const isHttps = window.location.protocol === 'https:'; + const host = window.location.host; + + if (isHttps) { + // 生产环境:通过 Nginx 代理 + return { + SSE: `wss://${host}/ws/sse`, // 上交所 - Nginx 代理 + SZSE: `wss://${host}/ws/szse`, // 深交所 - Nginx 代理 + }; + } + + // 开发环境:直连 + return { + SSE: 'ws://49.232.185.254:8765', // 上交所 + SZSE: 'ws://222.128.1.157:8765', // 深交所 + }; }; +/** WebSocket 服务地址 */ +export const WS_CONFIG: Record = getWsConfig(); + /** 心跳间隔 (ms) */ export const HEARTBEAT_INTERVAL = 30000; diff --git a/src/views/StockOverview/components/FlexScreen/hooks/useRealtimeQuote.ts b/src/views/StockOverview/components/FlexScreen/hooks/useRealtimeQuote.ts index 1167614a..73f243c2 100644 --- a/src/views/StockOverview/components/FlexScreen/hooks/useRealtimeQuote.ts +++ b/src/views/StockOverview/components/FlexScreen/hooks/useRealtimeQuote.ts @@ -2,8 +2,12 @@ * 实时行情 Hook * 管理上交所和深交所 WebSocket 连接,获取实时行情数据 * - * 上交所 (SSE): ws://49.232.185.254:8765 - 需主动订阅,提供五档行情 - * 深交所 (SZSE): ws://222.128.1.157:8765 - 自动推送,提供十档行情 + * 连接方式: + * - 生产环境 (HTTPS): 通过 Nginx 代理使用 wss:// (如 wss://valuefrontier.cn/ws/sse) + * - 开发环境 (HTTP): 直连 ws:// + * + * 上交所 (SSE): 需主动订阅,提供五档行情 + * 深交所 (SZSE): 自动推送,提供十档行情 */ import { useState, useEffect, useRef, useCallback } from 'react'; @@ -402,7 +406,8 @@ export const useRealtimeQuote = (codes: string[] = []): UseRealtimeQuoteReturn = }, []); const createConnection = useCallback((exchange: Exchange) => { - // 检查是否是 HTTPS 页面尝试连接 ws://(Mixed Content) + // 防御性检查:确保 HTTPS 页面不会意外连接 ws://(Mixed Content 安全错误) + // 正常情况下 WS_CONFIG 会自动根据协议返回正确的 URL,这里是备用保护 const isHttps = typeof window !== 'undefined' && window.location.protocol === 'https:'; const wsUrl = WS_CONFIG[exchange]; const isInsecureWs = wsUrl.startsWith('ws://'); @@ -410,7 +415,7 @@ export const useRealtimeQuote = (codes: string[] = []): UseRealtimeQuoteReturn = if (isHttps && isInsecureWs) { logger.warn( 'FlexScreen', - `${exchange} WebSocket 连接被跳过:HTTPS 页面无法连接不安全的 ws:// 端点` + `${exchange} WebSocket 配置错误:HTTPS 页面尝试连接 ws:// 端点,请检查 Nginx 代理配置` ); return; }