From 107ef111066319f68c375abfb83a00f70662ea6e Mon Sep 17 00:00:00 2001 From: xudan Date: Fri, 18 Jul 2025 15:03:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=AD=98=E5=82=A8Web?= =?UTF-8?q?Socket=E5=9F=BA=E7=A1=80=E8=B7=AF=E5=BE=84=E6=94=AF=E6=8C=81?= =?UTF-8?q?=EF=BC=8C=E6=9B=B4=E6=96=B0=E7=8E=AF=E5=A2=83=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=92=8CWebSocket=E6=9C=8D=E5=8A=A1=E4=BB=A5=E5=A4=84=E7=90=86?= =?UTF-8?q?=E6=96=B0=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 3 ++- .env.development | 1 + .env.production | 1 + src/services/ws.ts | 35 ++++++++++++++++++++++++----------- vite.config.ts | 6 ++++++ 5 files changed, 34 insertions(+), 12 deletions(-) diff --git a/.env b/.env index 6d2b543..4d3a08b 100644 --- a/.env +++ b/.env @@ -1,3 +1,4 @@ ENV_APP_TITLE=运输控制系统 ENV_HTTP_BASE=/api -ENV_WEBSOCKET_BASE=/ws \ No newline at end of file +ENV_WEBSOCKET_BASE=/ws +ENV_STORAGE_WEBSOCKET_BASE=/vwedWs \ No newline at end of file diff --git a/.env.development b/.env.development index a6bf65c..3f57635 100644 --- a/.env.development +++ b/.env.development @@ -1,6 +1,7 @@ ENV_APP_TITLE=运输控制系统(开发) # ENV_HTTP_BASE=/mocks ENV_WEBSOCKET_BASE=/ws +ENV_STORAGE_WEBSOCKET_BASE=/vwedWs # 开发环境token配置 - 可以手动设置或从另一个项目获取后填入 ENV_DEV_TOKEN=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3NTAzMzkwMTcsInVzZXJuYW1lIjoiYWRtaW4ifQ.uGWMIPH9-sdyEwr0bQBMKQSTAjYBZhlIVDRHGtheENE diff --git a/.env.production b/.env.production index 2a45f87..04352f9 100644 --- a/.env.production +++ b/.env.production @@ -1,3 +1,4 @@ ENV_APP_TITLE=大众一汽发动机AMR调度系统 ENV_HTTP_BASE=/jeecg-boot/ ENV_WEBSOCKET_BASE=/ws +ENV_STORAGE_WEBSOCKET_BASE=/vwedWs diff --git a/src/services/ws.ts b/src/services/ws.ts index 9b6c06d..542524e 100644 --- a/src/services/ws.ts +++ b/src/services/ws.ts @@ -119,7 +119,9 @@ class EnhancedWebSocket { if (isHeartbeatResponse) { this.heartbeatReceivedCount++; const responseTime = Date.now() - this.lastHeartbeatTime; - console.log(`💗 收到心跳响应: ${this.path}, 响应时间: ${responseTime}ms, 已发送: ${this.heartbeatSentCount}, 已接收: ${this.heartbeatReceivedCount}`); + console.log( + `💗 收到心跳响应: ${this.path}, 响应时间: ${responseTime}ms, 已发送: ${this.heartbeatSentCount}, 已接收: ${this.heartbeatReceivedCount}`, + ); // 心跳响应,不传递给业务代码 return; } @@ -133,17 +135,17 @@ class EnhancedWebSocket { this.ws.onclose = (event) => { const connectionDuration = Date.now() - this.connectionStartTime; const closeReason = getCloseReasonDescription(event.code, event.reason); - + console.log(`❌ WebSocket连接关闭: ${this.path}`); console.log(` └─ 关闭原因: ${closeReason}`); console.log(` └─ 连接持续时间: ${connectionDuration}ms`); console.log(` └─ 心跳统计: 发送${this.heartbeatSentCount}次, 接收${this.heartbeatReceivedCount}次`); console.log(` └─ 是否手动关闭: ${this.isManualClose}`); console.log(` └─ 是否心跳超时: ${this.isHeartbeatTimeout}`); - + // 分析断连原因 this.analyzeDisconnectionReason(event.code, event.reason); - + this.stopHeartbeat(); // 先调用业务代码的关闭处理 @@ -178,7 +180,7 @@ class EnhancedWebSocket { if (reason) { console.log(` └─ 服务器提供的关闭原因: ${reason}`); } - + switch (code) { case 1000: console.log(' └─ 正常关闭,可能是服务器主动关闭或客户端主动关闭'); @@ -222,11 +224,16 @@ class EnhancedWebSocket { // 获取连接状态文本 private getReadyStateText(): string { switch (this.ws.readyState) { - case WebSocket.CONNECTING: return 'CONNECTING(0)'; - case WebSocket.OPEN: return 'OPEN(1)'; - case WebSocket.CLOSING: return 'CLOSING(2)'; - case WebSocket.CLOSED: return 'CLOSED(3)'; - default: return `UNKNOWN(${this.ws.readyState})`; + case WebSocket.CONNECTING: + return 'CONNECTING(0)'; + case WebSocket.OPEN: + return 'OPEN(1)'; + case WebSocket.CLOSING: + return 'CLOSING(2)'; + case WebSocket.CLOSED: + return 'CLOSED(3)'; + default: + return `UNKNOWN(${this.ws.readyState})`; } } @@ -434,7 +441,13 @@ class EnhancedWebSocket { } function create(path: string): Promise { - const baseUrl = import.meta.env.ENV_WEBSOCKET_BASE ?? ''; + let baseUrl = ''; + if (path.includes(import.meta.env.ENV_STORAGE_WEBSOCKET_BASE)) { + baseUrl = ''; + } else { + baseUrl = import.meta.env.ENV_WEBSOCKET_BASE ?? ''; + } + const ws = new EnhancedWebSocket(path, baseUrl) as WebSocket; return new Promise((resolve, reject) => { diff --git a/vite.config.ts b/vite.config.ts index 00396a2..d99c6f2 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -56,6 +56,12 @@ export default ({ mode }: Record) => changeOrigin: true, ws: true, }, + '/vwedWs/': { + target: 'ws://192.168.189.206:8000/', + rewrite: (path) => path.replace(/^\/vwedWs/, ''), + changeOrigin: true, + ws: true, + }, }, }, });