修改ws重连状态位,增加标志
authorhcj <1224667075@qq.com>
Fri, 26 Dec 2025 07:40:05 +0000 (15:40 +0800)
committerhcj <1224667075@qq.com>
Fri, 26 Dec 2025 07:40:05 +0000 (15:40 +0800)
code/sdk/zhihui/websocket/os_ws/haisi_websocket.c

index 0cdbe5a..92fafbe 100644 (file)
@@ -93,6 +93,9 @@ static volatile ws_close_status_t g_ws_close_status = WS_CLOSE_NOT_EXECUTED; /*
 static volatile int g_ws_is_reconnecting = 0; /* 是否正在重连的标志位 */
 static uint32_t g_main_thread_interval_ms = 1000; /* 主线程循环间隔时长(毫秒),默认1000ms */
 
+/* 强制重连超时检测相关变量 */
+static uint32_t g_ws_force_reconnect_start_tick = 0;  /* 强制重连开始时间(tick) */
+
 /* 连接超时检测相关变量 */
 static time_t g_ws_connect_start_time = 0;  /* 连接开始时间 */
 static volatile int g_ws_connect_timeout_occurred = 0;  /* 超时标志 */
@@ -191,6 +194,8 @@ static void uapi_ws_on_open(int client)
     // 检查是否是从重连状态恢复的连接
     if (g_ws_is_reconnecting) {
         HAISI_WS_INFO("Reconnection successful, triggering reconnect success callback\n");
+        // 清除重连开始时间
+        g_ws_force_reconnect_start_tick = 0;
         g_ws_is_reconnecting = 0; // 重置重连标志
         
         // 触发重连成功回调
@@ -298,8 +303,10 @@ static void uapi_ws_on_close(int client)
     // 在断开回调触发时,将关闭状态设置为成功
  
     HAISI_WS_INFO("WebSocket close callback triggered, status set to SUCCESS (%d)\n", g_ws_close_status);
+
     if(g_ws_connected!=WS_STATE_SLEEP)
     g_ws_connected = WS_STATE_DISCONNECTED;
+
     HAISI_WS_DEBUG("连接状态: 未连接\n");
     
     // 连接断开时重置suspend状态和活动时间
@@ -313,11 +320,7 @@ static void uapi_ws_on_close(int client)
     if(g_ws_client_fd>=0)
     {
         // 这里不再调用 uapi_ws_close_with_status,因为回调已经触发说明关闭成功
-        if(g_ws_close_status==WS_CLOSE_NOT_EXECUTED)
-        {
-
-        }
-        else
+        if(g_ws_close_status!=WS_CLOSE_NOT_EXECUTED)
         {
             g_ws_client_fd=-1;
         }
@@ -342,7 +345,7 @@ static void uapi_ws_main_thread(void *arg)
     (void)arg;
     ws_msg_info_t *msg;
     uint32_t msgsize =0;
-    int last_connection_state = WS_STATE_DISCONNECTED;  // 记录上次连接状态
+int last_connection_state = WS_STATE_DISCONNECTED;  // 记录上次连接状态
     
     while (g_ws_running) {
         // 处理队列消息(非阻塞)
@@ -415,10 +418,10 @@ static void uapi_ws_main_thread(void *arg)
         
         // 检测连接状态变化并触发事件回调
         if (g_ws_callbacks.event_callback != NULL && g_ws_connected != last_connection_state) {
-            if (g_ws_connected == WS_STATE_CONNECTED && last_connection_state != WS_STATE_CONNECTED) {
+            if (g_ws_connected == WS_STATE_CONNECTED && last_connection_state != WS_STATE_CONNECTED && g_ws_is_reconnecting!=1 ) {
                 // 连接成功事件
                 g_ws_callbacks.event_callback(WS_EVENT_CONNECTED);
-            } else if (g_ws_connected == WS_STATE_DISCONNECTED && last_connection_state == WS_STATE_CONNECTED) {
+            } else if (g_ws_connected == WS_STATE_DISCONNECTED && last_connection_state == WS_STATE_CONNECTED && g_ws_is_reconnecting!=1) {
                 // 连接断开事件
                 g_ws_callbacks.event_callback(WS_EVENT_DISCONNECTED);
             }
@@ -436,11 +439,37 @@ static void uapi_ws_main_thread(void *arg)
         if (g_ws_connected == WS_STATE_RECONNECTING) {
             // 重连执行中状态:等待断开完成,然后开始重新连接
             HAISI_WS_DEBUG("Reconnecting mode: waiting for disconnect completion\n");
+            uint32_t current_tick = osKernelGetTickCount();
             if (g_ws_client_fd < 0) {
                 // 断开完成,开始重新连接
                 HAISI_WS_INFO("Disconnect completed, starting reconnection\n");
                 g_ws_connected = WS_STATE_DISCONNECTED;
-            } else {
+            } else if(g_ws_close_status== WS_CLOSE_NOT_EXECUTED){
+
+                HAISI_WS_DEBUG("WebSocketClientClose WS_CLOSE_NOT_EXECUTED\n");
+                uapi_ws_close_with_status(g_ws_client_fd);
+                g_ws_close_status = WS_CLOSE_NOT_EXECUTED;
+                g_ws_client_fd=-1;
+                HAISI_WS_DEBUG("WebSocketClientClose WS_CLOSE_NOT_EXECUTED end\n");
+
+            }
+            else if(current_tick-g_ws_force_reconnect_start_tick > 10000)
+            {
+            
+                    g_ws_connected = WS_STATE_DISCONNECTED;
+                    g_ws_is_reconnecting = 0;
+                    g_ws_force_reconnect_start_tick = 0;
+                    // 强制关闭连接
+                    if (g_ws_client_fd >= 0) {
+                        uapi_ws_close_with_status(g_ws_client_fd);
+                        g_ws_client_fd = -1;
+                    }
+                    // 重置关闭状态
+                    g_ws_close_status = WS_CLOSE_NOT_EXECUTED;
+                    HAISI_WS_INFO("Force reconnect state reset completed\n");
+            }
+            else
+            {
                 // 等待断开完成
                 usleep(100 * 1000); // 等待100ms
             }
@@ -702,13 +731,19 @@ int uapi_ws_send_text(const char *text)
     }
     
     // 发送前检查并resume
-    uapi_ws_resume();
+    // uapi_ws_resume();
     
     // 设置发送状态标志
     g_ws_is_sending = 1;
     
     // 发送数据
+    HAISI_WS_WARN("uapi_ws_send_text \n");
+
     int ret = WebSocketClientSendText(g_ws_client_fd, text, strlen(text));
+
+    HAISI_WS_WARN("uapi_ws_send_text end\n");
+
+
     
     // 更新最后发送时间
     if (ret == 0) {
@@ -795,8 +830,12 @@ int uapi_ws_force_reconnect(void)
 
     HAISI_WS_INFO("Starting WebSocket force reconnection\n");
     
+    // 记录重连开始时间(使用tick计数)
+    g_ws_force_reconnect_start_tick = osKernelGetTickCount();
+    
     // 设置重连执行中状态和重连标志
     g_ws_connected = WS_STATE_RECONNECTING;
+    
     g_ws_is_reconnecting = 1; // 标记正在重连
 
     if (g_ws_client_fd >= 0) {
@@ -804,7 +843,7 @@ int uapi_ws_force_reconnect(void)
         uapi_ws_close_with_status(g_ws_client_fd);
     }
     
-    HAISI_WS_INFO("WebSocket force reconnection initiated\n");
+    HAISI_WS_INFO("WebSocket force reconnection initiated, start tick: %u\n", g_ws_force_reconnect_start_tick);
     return 0;
 }