Skip to content

Commit

Permalink
Update simple websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
gelanchez committed Oct 5, 2020
1 parent 43eb1a0 commit a190cbf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ ESP32 libraries:
- [WiFi.h](https://github.com/espressif/arduino-esp32/tree/master/libraries/WiFi)

External libraries:
- [AsyncTCP](https://github.com/me-no-dev/AsyncTCP). Async TCP Library for ESP32.
- [ArduinoJson](https://arduinojson.org). JSON library for embedded C++.
- [AsyncTCP](https://github.com/me-no-dev/AsyncTCP). Async TCP Library for ESP32.
- [ESPAsyncWebServer](https://github.com/me-no-dev/ESPAsyncWebServer). Async HTTP and WebSocket Server.

## Usage
Expand Down
37 changes: 24 additions & 13 deletions src/webserver.ino
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,7 @@ AsyncWebSocket g_ws("/ws");

static unsigned long g_lastUpdate = millis();

void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){
if(type == WS_EVT_CONNECT)
{
Serial.println("Websocket client connection received");
}
else if(type == WS_EVT_DISCONNECT)
{
Serial.println("Client disconnected");
}
else if(type == WS_EVT_DATA)
Serial.println("Data received: ");
}
void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len);

void setup()
{
Expand All @@ -64,6 +53,7 @@ void setup()
/**
* @brief Server setup.
*/

g_server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", MAIN_page);
});
Expand Down Expand Up @@ -105,4 +95,25 @@ void loop()

g_ws.cleanupClients();
}
}
}

void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len)
{
if(type == WS_EVT_CONNECT)
{
Serial.println("Websocket client connection received");
}
else if(type == WS_EVT_DISCONNECT)
{
Serial.println("Client disconnected");
}
else if(type == WS_EVT_DATA)
{
Serial.println("Data received: ");
for(int i=0; i < len; i++)
{
Serial.print((char) data[i]);
}
Serial.println();
}
}

0 comments on commit a190cbf

Please sign in to comment.