From 0c9489ad19b7f98a2b54dd13fef255959cfe3920 Mon Sep 17 00:00:00 2001 From: hcj <1224667075@qq.com> Date: Wed, 13 Aug 2025 21:39:00 +0800 Subject: [PATCH] =?utf8?q?=E4=BF=AE=E5=A4=8D=E6=8F=90=E5=89=8D=E5=90=AF?= =?utf8?q?=E5=8A=A8websocket=E5=AF=BC=E8=87=B4=E7=BA=BF=E7=A8=8B=E6=97=A0?= =?utf8?q?=E6=B3=95=E9=80=80=E5=87=BA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../slip/cat1Service/cat1_ai_server.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/code/sdk/drivers/peripheral/slip/cat1Service/cat1_ai_server.cpp b/code/sdk/drivers/peripheral/slip/cat1Service/cat1_ai_server.cpp index 017c92c2c..7b3b92607 100644 --- a/code/sdk/drivers/peripheral/slip/cat1Service/cat1_ai_server.cpp +++ b/code/sdk/drivers/peripheral/slip/cat1Service/cat1_ai_server.cpp @@ -90,6 +90,11 @@ extern "C" { #define AI_MAX_RETRY_COUNT 3 // 最大重试次数 #define AI_QUEUE_CHECK_INTERVAL 1000 // 队列检查间隔(ms) +// WebSocket超时配置 +#define AI_WS_TCP_CONNECTION_TIMEOUT_MS 15000 // 15秒TCP连接确认超时 +#define AI_WS_HANDSHAKE_TIMEOUT_MS 10000 // 10秒握手等待超时 +#define AI_WS_TCP_CHECK_INTERVAL_MS 100 // TCP状态检查间隔(ms) + // 资源保护 static osMutexId_t g_ai_mutex = NULL; static uint32_t g_ai_memory_usage = 0; @@ -189,7 +194,7 @@ static bool wait_for_tcp_connection(WebSocketClient* client, const char* client_ } uint32_t start_time = (uint32_t)osKernelGetTickCount(); - uint32_t check_interval = 100; // 每100ms检查一次 + uint32_t check_interval = AI_WS_TCP_CHECK_INTERVAL_MS; // 使用宏定义的检查间隔 WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "%s 等待TCP连接建立确认...", client_type); @@ -1004,7 +1009,7 @@ void bk_websocket_demo(void *data) { tcp_connection_state_t current_state = tcp_get_connection_state(chat_client->tcp_handle); WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "Chat WebSocket: 当前TCP连接状态: %s", tcp_state_to_string(current_state)); - if (!wait_for_tcp_connection(chat_client, "Chat", 15000)) { // 15秒TCP连接确认超时 + if (!wait_for_tcp_connection(chat_client, "Chat", AI_WS_TCP_CONNECTION_TIMEOUT_MS)) { // 使用宏定义的TCP连接确认超时 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Chat WebSocket: TCP连接确认失败,退出线程"); lwip_close_websocket_client(chat_client); chat_client = NULL; @@ -1014,7 +1019,7 @@ void bk_websocket_demo(void *data) { // 等待WebSocket握手完成,增加超时机制 uint32_t handshake_start_time = (uint32_t)osKernelGetTickCount(); - const uint32_t handshake_timeout_ms = 10000; // 30秒超时 + const uint32_t handshake_timeout_ms = AI_WS_HANDSHAKE_TIMEOUT_MS; // 使用宏定义的握手超时 while (chat_client->handshake == 0) { // 检查是否超时 @@ -1514,7 +1519,7 @@ msleep(1000); tcp_connection_state_t current_state = tcp_get_connection_state(asr_client->tcp_handle); WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "ASR WebSocket: 当前TCP连接状态: %s", tcp_state_to_string(current_state)); - if (!wait_for_tcp_connection(asr_client, "ASR", 15000)) { // 15秒TCP连接确认超时 + if (!wait_for_tcp_connection(asr_client, "ASR", AI_WS_TCP_CONNECTION_TIMEOUT_MS)) { // 使用宏定义的TCP连接确认超时 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ASR WebSocket: TCP连接确认失败,退出线程"); lwip_close_websocket_client(asr_client); asr_client = NULL; @@ -1524,7 +1529,7 @@ msleep(1000); // 等待ASR WebSocket握手完成,增加超时机制 uint32_t asr_handshake_start_time = (uint32_t)osKernelGetTickCount(); - const uint32_t asr_handshake_timeout_ms = 10000; // 30秒超时 + const uint32_t asr_handshake_timeout_ms = AI_WS_HANDSHAKE_TIMEOUT_MS; // 使用宏定义的握手超时 while (asr_client->handshake == 0) { // 检查是否超时 @@ -1967,7 +1972,7 @@ void bk_websocket_tts(void *data) { tcp_connection_state_t current_state = tcp_get_connection_state(tts_client->tcp_handle); WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "TTS WebSocket: 当前TCP连接状态: %s", tcp_state_to_string(current_state)); - if (!wait_for_tcp_connection(tts_client, "TTS", 15000)) { // 15秒TCP连接确认超时 + if (!wait_for_tcp_connection(tts_client, "TTS", AI_WS_TCP_CONNECTION_TIMEOUT_MS)) { // 使用宏定义的TCP连接确认超时 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "TTS WebSocket: TCP连接确认失败,退出线程"); lwip_close_websocket_client(tts_client); tts_client = NULL; @@ -1977,7 +1982,7 @@ void bk_websocket_tts(void *data) { // 等待TTS WebSocket握手完成,增加超时机制 uint32_t tts_handshake_start_time = (uint32_t)osKernelGetTickCount(); - const uint32_t tts_handshake_timeout_ms = 10; // 30秒超时 + const uint32_t tts_handshake_timeout_ms = AI_WS_HANDSHAKE_TIMEOUT_MS; // 使用宏定义的握手超时 while (tts_client->handshake == 0) { // 检查是否超时 -- 2.17.1