Skip to content

Commit 7449284

Browse files
committed
feat(websocket): Create specific Event for PONG message
- Separate the PONG message from generic payload, this can make it easier to identify when a timeout occur when receiving a payload
1 parent 39e2333 commit 7449284

File tree

5 files changed

+34
-2
lines changed

5 files changed

+34
-2
lines changed

components/esp_websocket_client/esp_websocket_client.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,11 @@ static esp_err_t esp_websocket_client_recv(esp_websocket_client_handle_t client)
984984
return ESP_OK;
985985
}
986986

987-
esp_websocket_client_dispatch_event(client, WEBSOCKET_EVENT_DATA, client->rx_buffer, rlen);
987+
if (client->last_opcode == WS_TRANSPORT_OPCODES_PONG) {
988+
esp_websocket_client_dispatch_event(client, WEBSOCKET_EVENT_PONG, client->rx_buffer, rlen);
989+
} else {
990+
esp_websocket_client_dispatch_event(client, WEBSOCKET_EVENT_DATA, client->rx_buffer, rlen);
991+
}
988992

989993
client->payload_offset += rlen;
990994
} while (client->payload_offset < client->payload_len);

components/esp_websocket_client/examples/linux/main/websocket_linux.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ static void websocket_event_handler(void *handler_args, esp_event_base_t base, i
3030
break;
3131
case WEBSOCKET_EVENT_CONNECTED:
3232
ESP_LOGI(TAG, "WEBSOCKET_EVENT_CONNECTED");
33+
esp_websocket_client_send_with_opcode((esp_websocket_client_handle_t)handler_args, WS_TRANSPORT_OPCODES_PING, NULL, 0, portMAX_DELAY);
3334
break;
3435
case WEBSOCKET_EVENT_DISCONNECTED:
3536
ESP_LOGI(TAG, "WEBSOCKET_EVENT_DISCONNECTED");
@@ -52,6 +53,9 @@ static void websocket_event_handler(void *handler_args, esp_event_base_t base, i
5253
// If received data contains json structure it succeed to parse
5354
ESP_LOGW(TAG, "Total payload length=%d, data_len=%d, current payload offset=%d\r\n", data->payload_len, data->data_len, data->payload_offset);
5455

56+
break;
57+
case WEBSOCKET_EVENT_PONG:
58+
ESP_LOGI(TAG, "WEBSOCKET_EVENT_PONG (op=%d, len=%d)", data->op_code, data->data_len);
5559
break;
5660
case WEBSOCKET_EVENT_ERROR:
5761
ESP_LOGI(TAG, "WEBSOCKET_EVENT_ERROR");

components/esp_websocket_client/examples/target/main/websocket_example.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ static void websocket_event_handler(void *handler_args, esp_event_base_t base, i
7979
break;
8080
case WEBSOCKET_EVENT_CONNECTED:
8181
ESP_LOGI(TAG, "WEBSOCKET_EVENT_CONNECTED");
82+
esp_websocket_client_send_with_opcode((esp_websocket_client_handle_t)handler_args, WS_TRANSPORT_OPCODES_PING, NULL, 0, portMAX_DELAY);
8283
break;
8384
case WEBSOCKET_EVENT_DISCONNECTED:
8485
ESP_LOGI(TAG, "WEBSOCKET_EVENT_DISCONNECTED");
@@ -114,6 +115,12 @@ static void websocket_event_handler(void *handler_args, esp_event_base_t base, i
114115

115116
ESP_LOGW(TAG, "Total payload length=%d, data_len=%d, current payload offset=%d\r\n", data->payload_len, data->data_len, data->payload_offset);
116117

118+
xTimerReset(shutdown_signal_timer, portMAX_DELAY);
119+
break;
120+
case WEBSOCKET_EVENT_PONG:
121+
ESP_LOGI(TAG, "WEBSOCKET_EVENT_PONG");
122+
ESP_LOGI(TAG, "Received opcode=%d", data->op_code);
123+
// Reset shutdown timer on PONG as well
117124
xTimerReset(shutdown_signal_timer, portMAX_DELAY);
118125
break;
119126
case WEBSOCKET_EVENT_ERROR:

components/esp_websocket_client/examples/target/pytest_websocket.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,23 @@ def test_examples_protocol_websocket(dut):
8989
3. send and receive data
9090
"""
9191

92+
# Test for connection:
93+
# Verifies that the WebSocket client correctly connected.
94+
def test_connected(dut):
95+
dut.expect('WEBSOCKET_EVENT_CONNECTED')
96+
print('Received CONNECTED')
97+
98+
# Test for PONG functionality:
99+
# Verifies that the WebSocket client correctly receives a PONG response after sending a PING.
100+
# The PING is automatically sent when the connection is established.
101+
def test_pong(dut):
102+
dut.expect('WEBSOCKET_EVENT_PONG')
103+
print('Received PONG')
104+
92105
# Test for echo functionality:
93106
# Sends a series of simple "hello" messages to the WebSocket server and verifies that each one is echoed back correctly.
94107
# This tests the basic responsiveness and correctness of the WebSocket connection.
95108
def test_echo(dut):
96-
dut.expect('WEBSOCKET_EVENT_CONNECTED')
97109
for i in range(0, 5):
98110
dut.expect(re.compile(b'Received=hello (\\d)'))
99111
print('All echos received')
@@ -236,6 +248,8 @@ def test_fragmented_binary_msg(dut):
236248
print('DUT connecting to {}'.format(uri))
237249
dut.expect('Please enter uri of websocket endpoint', timeout=30)
238250
dut.write(uri)
251+
test_connected(dut)
252+
test_pong(dut)
239253
test_echo(dut)
240254
test_recv_long_msg(dut, ws, 2000, 3)
241255
test_json(dut, ws)
@@ -246,4 +260,6 @@ def test_fragmented_binary_msg(dut):
246260
test_close(dut)
247261
else:
248262
print('DUT connecting to {}'.format(uri))
263+
test_connected(dut)
264+
test_pong(dut)
249265
test_echo(dut)

components/esp_websocket_client/include/esp_websocket_client.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ typedef enum {
3434
WEBSOCKET_EVENT_CONNECTED, /*!< Once the Websocket has been connected to the server, no data exchange has been performed */
3535
WEBSOCKET_EVENT_DISCONNECTED, /*!< The connection has been disconnected */
3636
WEBSOCKET_EVENT_DATA, /*!< When receiving data from the server, possibly multiple portions of the packet */
37+
WEBSOCKET_EVENT_PONG, /*!< When receiving a PONG control frame from the server */
3738
WEBSOCKET_EVENT_CLOSED, /*!< The connection has been closed cleanly */
3839
WEBSOCKET_EVENT_BEFORE_CONNECT, /*!< The event occurs before connecting */
3940
WEBSOCKET_EVENT_BEGIN, /*!< The event occurs once after thread creation, before event loop */

0 commit comments

Comments
 (0)