diff --git a/src/core/http.cpp b/src/core/http.cpp index 0396ad5..907c62d 100644 --- a/src/core/http.cpp +++ b/src/core/http.cpp @@ -6,6 +6,8 @@ AsyncWebServer server(80); +AsyncWebSocket ws("/ws"); + void httpIndex(AsyncWebServerRequest *request) { info(request->url().c_str()); request->send(LittleFS, "/index.html", "text/html"); @@ -54,6 +56,32 @@ void httpActionConfirm(AsyncWebServerRequest *request) { } void httpSetup() { + ws.onEvent([](AsyncWebSocket *socket, AsyncWebSocketClient *client, AwsEventType type, void *arg, unsigned char *message, unsigned length) { + const char *t; + switch (type) { + case WS_EVT_CONNECT: + t = "CONNECT"; + break; + case WS_EVT_DISCONNECT: + t = "DISCONNECT"; + break; + case WS_EVT_PONG: + t = "PONG"; + break; + case WS_EVT_ERROR: + t = "ERROR"; + break; + case WS_EVT_DATA: + t = "DATA"; + break; + default: + t = "[???]"; + break; + } + debug("%s: %s (%d bytes)", client->remoteIP().toString().c_str(), t, length); + }); + server.addHandler(&ws); + server.on("/", HTTP_GET, httpIndex); server.on("/action/left", HTTP_GET, httpActionLeft); server.on("/action/up", HTTP_GET, httpActionUp); @@ -63,3 +91,7 @@ void httpSetup() { server.on("/action/confirm", HTTP_GET, httpActionConfirm); server.begin(); } + +void httpLoop() { + ws.cleanupClients(); +} diff --git a/src/core/http.h b/src/core/http.h index abcba1f..81f29da 100644 --- a/src/core/http.h +++ b/src/core/http.h @@ -3,4 +3,6 @@ void httpSetup(); +void httpLoop(); + #endif diff --git a/src/main.cpp b/src/main.cpp index c8002c1..24e6838 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,4 +18,5 @@ void loop() { logLoop(); wifiLoop(); appLoop(); + httpLoop(); }