Skip to content

Commit 317701b

Browse files
committed
Websockets: Pass pong data to callback
1 parent 06afdb6 commit 317701b

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

examples/arduino/WebSocket/WebSocket.ino

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,14 @@ void setup() {
103103
Serial.println("ws error");
104104

105105
} else if (type == WS_EVT_PONG) {
106-
Serial.println("ws pong");
107-
106+
if (len == sizeof(uint32_t)) {
107+
uint32_t pongTime;
108+
memcpy(&pongTime, data, sizeof(pongTime));
109+
uint32_t pingDuration = micros() - pongTime;
110+
Serial.printf("ws[%" PRIu32 "] pong [%" PRIu32 "] duration: %" PRIu32 " us\n", client->id(), pongTime, pingDuration);
111+
} else {
112+
Serial.printf("ws[%" PRIu32 "] pong [%u]\n", client->id(), len);
113+
}
108114
} else if (type == WS_EVT_DATA) {
109115
AwsFrameInfo *info = (AwsFrameInfo *)arg;
110116
Serial.printf(
@@ -205,8 +211,10 @@ void loop() {
205211
ws.textAll(random);
206212

207213
// ping twice (2 control frames)
208-
ws.pingAll();
209-
ws.pingAll();
214+
uint32_t pingTime = micros();
215+
ws.pingAll((const uint8_t *)&pingTime, sizeof(pingTime));
216+
pingTime = micros();
217+
ws.pingAll((const uint8_t *)&pingTime, sizeof(pingTime));
210218

211219
#ifdef ESP32
212220
Serial.printf("Uptime: %3lu s, Free heap: %" PRIu32 ", Min free heap: %" PRIu32 "\n", millis() / 1000, ESP.getFreeHeap(), ESP.getMinFreeHeap());

src/AsyncWebSocket.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ void AsyncWebSocketClient::_onData(void *pbuf, size_t plen) {
635635
} else if (_pinfo.opcode == WS_PONG) {
636636
async_ws_log_v("[%s][%" PRIu32 "] DATA PONG", _server->url(), _clientId);
637637
if (datalen != AWSC_PING_PAYLOAD_LEN || memcmp(AWSC_PING_PAYLOAD, data, AWSC_PING_PAYLOAD_LEN) != 0) {
638-
_server->_handleEvent(this, WS_EVT_PONG, NULL, NULL, 0);
638+
_server->_handleEvent(this, WS_EVT_PONG, NULL, data, datalen);
639639
}
640640

641641
} else if (_pinfo.opcode < WS_DISCONNECT) { // continuation or text/binary frame

0 commit comments

Comments
 (0)