@@ -74,6 +74,7 @@ static const char *TAG = "websocket_client";
7474const static int STOPPED_BIT = BIT0 ;
7575const static int CLOSE_FRAME_SENT_BIT = BIT1 ; // Indicates that a close frame was sent by the client
7676// and we are waiting for the server to continue with clean close
77+ const static int REQUESTED_STOP_BIT = BIT2 ; // Indicates that a client stop has been requested
7778
7879ESP_EVENT_DEFINE_BASE (WEBSOCKET_EVENTS );
7980
@@ -477,6 +478,7 @@ static esp_err_t stop_wait_task(esp_websocket_client_handle_t client)
477478 }
478479
479480 client -> run = false;
481+ xEventGroupSetBits (client -> status_bits , REQUESTED_STOP_BIT );
480482 xEventGroupWaitBits (client -> status_bits , STOPPED_BIT , false, true, portMAX_DELAY );
481483 client -> state = WEBSOCKET_STATE_UNKNOW ;
482484 return ESP_OK ;
@@ -1199,8 +1201,8 @@ static void esp_websocket_client_task(void *pv)
11991201 }
12001202 }
12011203 } else if (WEBSOCKET_STATE_WAIT_TIMEOUT == client -> state ) {
1202- // waiting for reconnecting ...
1203- vTaskDelay ( client -> wait_timeout_ms / 2 / portTICK_PERIOD_MS );
1204+ // waiting for reconnection or a request to stop the client ...
1205+ xEventGroupWaitBits ( client -> status_bits , REQUESTED_STOP_BIT , false, true, client -> wait_timeout_ms / 2 / portTICK_PERIOD_MS );
12041206 } else if (WEBSOCKET_STATE_CLOSING == client -> state &&
12051207 (CLOSE_FRAME_SENT_BIT & xEventGroupGetBits (client -> status_bits ))) {
12061208 ESP_LOGD (TAG , " Waiting for TCP connection to be closed by the server" );
@@ -1262,7 +1264,7 @@ esp_err_t esp_websocket_client_start(esp_websocket_client_handle_t client)
12621264 ESP_LOGE (TAG , "Error create websocket task" );
12631265 return ESP_FAIL ;
12641266 }
1265- xEventGroupClearBits (client -> status_bits , STOPPED_BIT | CLOSE_FRAME_SENT_BIT );
1267+ xEventGroupClearBits (client -> status_bits , STOPPED_BIT | CLOSE_FRAME_SENT_BIT | REQUESTED_STOP_BIT );
12661268 ESP_LOGI (TAG , "Started" );
12671269 return ESP_OK ;
12681270}
@@ -1331,6 +1333,7 @@ static esp_err_t esp_websocket_client_close_with_optional_body(esp_websocket_cli
13311333
13321334 // If could not close gracefully within timeout, stop the client and disconnect
13331335 client -> run = false;
1336+ xEventGroupSetBits (client -> status_bits , REQUESTED_STOP_BIT );
13341337 xEventGroupWaitBits (client -> status_bits , STOPPED_BIT , false, true, portMAX_DELAY );
13351338 client -> state = WEBSOCKET_STATE_UNKNOW ;
13361339 return ESP_OK ;
0 commit comments