Skip to content

Commit

Permalink
Merge pull request #564 from KipK/pingpong
Browse files Browse the repository at this point in the history
ping pong over websocket
  • Loading branch information
jeremypoulter authored Mar 10, 2023
2 parents 301f390 + 93f0924 commit cf8601a
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/web_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,17 @@ void handleNotFound(MongooseHttpServerRequest *request)
void onWsFrame(MongooseHttpWebSocketConnection *connection, int flags, uint8_t *data, size_t len)
{
DBUGF("Got message %.*s", len, (const char *)data);
const size_t capacity = JSON_OBJECT_SIZE(1) + 16;
DynamicJsonDocument doc(capacity);
DeserializationError error = deserializeJson(doc, data, len);
if (!error) {
if (doc.containsKey("ping") && doc["ping"].is<int8_t>())
{
// answer pong
connection->send("{\"pong\": 1}");

}
}
}

void onWsConnect(MongooseHttpWebSocketConnection *connection)
Expand Down

0 comments on commit cf8601a

Please sign in to comment.