websocket

This commit is contained in:
Patrick Haßel 2025-01-09 11:04:19 +01:00
parent ff14910c89
commit 0e93eafb7e
3 changed files with 35 additions and 0 deletions

View File

@ -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();
}

View File

@ -3,4 +3,6 @@
void httpSetup();
void httpLoop();
#endif

View File

@ -18,4 +18,5 @@ void loop() {
logLoop();
wifiLoop();
appLoop();
httpLoop();
}