修复了WS连接稳定性问题

This commit is contained in:
CSBOOK\44744
2026-06-27 20:29:09 +08:00
parent b829cd8ebc
commit caa4a7eded
16 changed files with 2197 additions and 248 deletions
@@ -156,7 +156,7 @@ static bool s_connected;
static bool s_stop_requested;
static portMUX_TYPE s_client_state_lock = portMUX_INITIALIZER_UNLOCKED;
static char s_bearer_token[MULTIMODAL_WS_TOKEN_MAX_LEN];
static char s_headers[384];
static char s_headers[768];
static multimodal_ws_event_callback_t s_event_cb;
static void *s_event_cb_ctx;
@@ -32,7 +32,9 @@ static void websocket_event_handler(void *handler_args,
stop_requested ? "WebSocket disconnected (local stop)" :
"WebSocket disconnected");
ws_set_stop_requested(false);
emit_event(MULTIMODAL_WS_EVENT_DISCONNECTED, NULL, 0U);
if (!stop_requested) {
emit_event(MULTIMODAL_WS_EVENT_DISCONNECTED, NULL, 0U);
}
break;
}
+20 -18
View File
@@ -254,39 +254,41 @@ esp_err_t wifi_portal_start_config_ap(const char *ssid_prefix)
esp_err_t wifi_portal_connect_sta(int timeout_ms)
{
ESP_RETURN_ON_ERROR(wifi_portal_lock(), TAG, "Failed to lock Wi-Fi portal");
PortalUnlockGuard guard;
{
ESP_RETURN_ON_ERROR(wifi_portal_lock(), TAG, "Failed to lock Wi-Fi portal");
PortalUnlockGuard guard;
if (!wifi_portal_has_saved_credentials()) {
return ESP_ERR_NOT_FOUND;
}
if (!wifi_portal_has_saved_credentials()) {
return ESP_ERR_NOT_FOUND;
}
ESP_LOGI(TAG,
"Starting STA connection with %u saved credential candidate(s)",
(unsigned)SsidManager::GetInstance().GetSsidList().size());
log_saved_wifi_candidates("STA connect");
ESP_LOGI(TAG,
"Starting STA connection with %u saved credential candidate(s)",
(unsigned)SsidManager::GetInstance().GetSsidList().size());
log_saved_wifi_candidates("STA connect");
ESP_RETURN_ON_ERROR(ensure_wifi_manager_initialized(), TAG, "Failed to initialize Wi-Fi manager");
ESP_RETURN_ON_ERROR(ensure_wifi_manager_initialized(), TAG, "Failed to initialize Wi-Fi manager");
auto &manager = WifiManager::GetInstance();
if (manager.IsConnected()) {
auto &manager = WifiManager::GetInstance();
if (manager.IsConnected()) {
(void)apply_sta_stability_policy();
return ESP_OK;
}
manager.StartStation();
(void)apply_sta_stability_policy();
return ESP_OK;
}
manager.StartStation();
(void)apply_sta_stability_policy();
int effective_timeout_ms = normalize_connect_timeout_ms(timeout_ms);
int64_t deadline_us = esp_timer_get_time() + (int64_t)effective_timeout_ms * 1000;
while (esp_timer_get_time() < deadline_us) {
if (manager.IsConnected()) {
if (WifiManager::GetInstance().IsConnected()) {
return ESP_OK;
}
vTaskDelay(pdMS_TO_TICKS(WIFI_PORTAL_CONNECT_POLL_MS));
}
manager.StopStation();
WifiManager::GetInstance().StopStation();
ESP_LOGW(TAG, "Wi-Fi connect timed out after %d ms", effective_timeout_ms);
return ESP_ERR_TIMEOUT;
}