1
0
mirror of https://github.com/s00500/ESPUI.git synced 2024-11-22 09:10:54 +00:00

Closes #12 implementing all events for websockets

This commit is contained in:
Lukas Bachschwell 2018-05-13 19:44:56 +02:00
parent cc633a7c85
commit f1012b2fe2

View File

@ -183,10 +183,21 @@ void ESPUIClass::prepareFileSystem() {
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
AwsEventType type, void *arg, uint8_t *data, size_t len) { AwsEventType type, void *arg, uint8_t *data, size_t len) {
switch (type) { switch (type) {
case WS_EVT_DISCONNECT: case WS_EVT_DISCONNECT: {
if (debug) if (debug)
Serial.printf("Disconnected!\n"); Serial.printf("Disconnected!\n");
break; break;
}
case WS_EVT_PONG: {
if (debug)
Serial.printf("Received PONG!\n");
break;
}
case WS_EVT_ERROR: {
if (debug)
Serial.printf("WebSocket Error!\n");
break;
}
case WS_EVT_CONNECT: { case WS_EVT_CONNECT: {
if (debug) { if (debug) {
Serial.print("Connected: "); Serial.print("Connected: ");
@ -198,19 +209,20 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
Serial.println("JSON Data Sent to Client!"); Serial.println("JSON Data Sent to Client!");
} }
} break; } break;
case WS_EVT_DATA: case WS_EVT_DATA: {
String msg = ""; String msg = "";
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {
msg += (char)data[i]; msg += (char)data[i];
} }
int id = msg.substring(msg.lastIndexOf(':') + 1).toInt(); int id = msg.substring(msg.lastIndexOf(':') + 1).toInt();
if (id >= ESPUI.cIndex) { if (id >= ESPUI.cIndex) {
if (debug) if (debug)
Serial.println("Maleformated id in websocket message"); Serial.println("Maleformated id in websocket message");
return; return;
} }
Control *c =
ESPUI.controls[msg.substring(msg.lastIndexOf(':') + 1).toInt()]; Control *c = ESPUI.controls[msg.substring(msg.lastIndexOf(':') + 1).toInt()];
if (msg.startsWith("bdown:")) { if (msg.startsWith("bdown:")) {
c->callback(*c, B_DOWN); c->callback(*c, B_DOWN);
@ -248,6 +260,9 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
ESPUI.updateSlider(c->id, value, client->id()); ESPUI.updateSlider(c->id, value, client->id());
c->callback(*c, SL_VALUE); c->callback(*c, SL_VALUE);
} }
}
break;
default:
break; break;
} }
} }