@@ -74,6 +74,7 @@ static const char *TAG = "websocket_client";
74
74
const static int STOPPED_BIT = BIT0 ;
75
75
const static int CLOSE_FRAME_SENT_BIT = BIT1 ; // Indicates that a close frame was sent by the client
76
76
// 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
77
78
78
79
ESP_EVENT_DEFINE_BASE (WEBSOCKET_EVENTS );
79
80
@@ -477,6 +478,7 @@ static esp_err_t stop_wait_task(esp_websocket_client_handle_t client)
477
478
}
478
479
479
480
client -> run = false;
481
+ xEventGroupSetBits (client -> status_bits , REQUESTED_STOP_BIT );
480
482
xEventGroupWaitBits (client -> status_bits , STOPPED_BIT , false, true, portMAX_DELAY );
481
483
client -> state = WEBSOCKET_STATE_UNKNOW ;
482
484
return ESP_OK ;
@@ -1199,8 +1201,8 @@ static void esp_websocket_client_task(void *pv)
1199
1201
}
1200
1202
}
1201
1203
} 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 );
1204
1206
} else if (WEBSOCKET_STATE_CLOSING == client -> state &&
1205
1207
(CLOSE_FRAME_SENT_BIT & xEventGroupGetBits (client -> status_bits ))) {
1206
1208
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)
1262
1264
ESP_LOGE (TAG , "Error create websocket task" );
1263
1265
return ESP_FAIL ;
1264
1266
}
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 );
1266
1268
ESP_LOGI (TAG , "Started" );
1267
1269
return ESP_OK ;
1268
1270
}
@@ -1331,6 +1333,7 @@ static esp_err_t esp_websocket_client_close_with_optional_body(esp_websocket_cli
1331
1333
1332
1334
// If could not close gracefully within timeout, stop the client and disconnect
1333
1335
client -> run = false;
1336
+ xEventGroupSetBits (client -> status_bits , REQUESTED_STOP_BIT );
1334
1337
xEventGroupWaitBits (client -> status_bits , STOPPED_BIT , false, true, portMAX_DELAY );
1335
1338
client -> state = WEBSOCKET_STATE_UNKNOW ;
1336
1339
return ESP_OK ;
0 commit comments