Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(websocket): add ability to reconnect after close #752

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions components/esp_websocket_client/esp_websocket_client.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -80,6 +80,7 @@ typedef struct {
char *auth;
int port;
bool auto_reconnect;
bool close_reconnect;
void *user_context;
int network_timeout_ms;
char *subprotocol;
Expand Down Expand Up @@ -376,6 +377,7 @@ static esp_err_t esp_websocket_client_set_config(esp_websocket_client_handle_t c
if (config->disable_auto_reconnect) {
cfg->auto_reconnect = false;
}
cfg->close_reconnect = config->enable_close_reconnect;

if (config->disable_pingpong_discon) {
cfg->pingpong_timeout_sec = 0;
Expand Down Expand Up @@ -1127,10 +1129,21 @@ static void esp_websocket_client_task(void *pv)
} else if (ret < 0) {
ESP_LOGW(TAG, "Connection terminated while waiting for clean TCP close");
}
client->run = false;
client->state = WEBSOCKET_STATE_UNKNOW;
esp_websocket_client_dispatch_event(client, WEBSOCKET_EVENT_CLOSED, NULL, 0);
break;
if (client->config->close_reconnect && xSemaphoreTakeRecursive(client->lock, lock_timeout) == pdPASS) {
client->state = WEBSOCKET_STATE_WAIT_TIMEOUT;
client->error_handle.error_type = WEBSOCKET_ERROR_TYPE_SERVER_CLOSE;
esp_transport_close(client->transport);
esp_websocket_client_dispatch_event(client, WEBSOCKET_EVENT_CLOSED, NULL, 0);
client->reconnect_tick_ms = _tick_get_ms();
ESP_LOGI(TAG, "Reconnect after %d ms", client->wait_timeout_ms);
xEventGroupClearBits(client->status_bits, STOPPED_BIT | CLOSE_FRAME_SENT_BIT);
xSemaphoreGiveRecursive(client->lock);
} else {
client->run = false;
client->state = WEBSOCKET_STATE_UNKNOW;
esp_websocket_client_dispatch_event(client, WEBSOCKET_EVENT_CLOSED, NULL, 0);
break;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ typedef enum {
WEBSOCKET_ERROR_TYPE_NONE = 0,
WEBSOCKET_ERROR_TYPE_TCP_TRANSPORT,
WEBSOCKET_ERROR_TYPE_PONG_TIMEOUT,
WEBSOCKET_ERROR_TYPE_HANDSHAKE
WEBSOCKET_ERROR_TYPE_HANDSHAKE,
WEBSOCKET_ERROR_TYPE_SERVER_CLOSE
} esp_websocket_error_type_t;

/**
Expand Down Expand Up @@ -101,6 +102,7 @@ typedef struct {
const char *password; /*!< Using for Http authentication */
const char *path; /*!< HTTP Path, if not set, default is `/` */
bool disable_auto_reconnect; /*!< Disable the automatic reconnect function when disconnected */
bool enable_close_reconnect; /*!< Enable reconnect after server close */
void *user_context; /*!< HTTP user data context */
int task_prio; /*!< Websocket task priority */
const char *task_name; /*!< Websocket task name */
Expand Down
Loading