mirror of
https://github.com/HendrikRauh/dmx-interface.git
synced 2025-12-16 07:34:09 +00:00
add WebSocket support for real-time status updates
This commit is contained in:
parent
c0da3a6af1
commit
e256d16f8d
8 changed files with 112 additions and 30 deletions
52
src/websocket.cpp
Normal file
52
src/websocket.cpp
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
#include "websocket.h"
|
||||
|
||||
AsyncWebSocket ws("/ws");
|
||||
|
||||
long webSocketLastUpdate = 0;
|
||||
const int WS_UPDATE_INTERVAL = 10 * 1000; // 10 seconds
|
||||
|
||||
String buildStatusString()
|
||||
{
|
||||
JsonDocument doc;
|
||||
doc["type"] = "status";
|
||||
doc["data"] = buildStatusJson();
|
||||
|
||||
String jsonString = "";
|
||||
serializeJson(doc, jsonString);
|
||||
return jsonString;
|
||||
}
|
||||
|
||||
void onEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case WS_EVT_CONNECT:
|
||||
Serial.printf("[WS] Client %u connected from %s\n", client->id(), client->remoteIP().toString().c_str());
|
||||
// directly send status to client
|
||||
ws.text(client->id(), buildStatusString());
|
||||
break;
|
||||
case WS_EVT_DISCONNECT:
|
||||
Serial.printf("[WS] Client %u disconnected\n", client->id());
|
||||
break;
|
||||
case WS_EVT_DATA:
|
||||
Serial.printf("[WS] Data received from client %u: %s\n", client->id(), (char *)data);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void webSocketLoop()
|
||||
{
|
||||
if (millis() - webSocketLastUpdate > WS_UPDATE_INTERVAL)
|
||||
{
|
||||
ws.textAll(buildStatusString());
|
||||
webSocketLastUpdate = millis();
|
||||
}
|
||||
}
|
||||
|
||||
void initWebSocket(AsyncWebServer *server)
|
||||
{
|
||||
ws.onEvent(onEvent);
|
||||
server->addHandler(&ws);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue