forked from tbnobody/OpenDTU
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#pragma once | ||
|
||
#include <ESPAsyncWebServer.h> | ||
|
||
class WebApiClass { | ||
public: | ||
WebApiClass(); | ||
void init(); | ||
|
||
private: | ||
AsyncWebServer _server; | ||
AsyncWebSocket _ws; | ||
AsyncEventSource _events; | ||
|
||
void onWebsocketEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len); | ||
|
||
void onNotFound(AsyncWebServerRequest* request); | ||
}; | ||
|
||
extern WebApiClass WebApi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include "WebApi.h" | ||
#include "defaults.h" | ||
#include <LittleFS.h> | ||
|
||
WebApiClass::WebApiClass() | ||
: _server(HTTP_PORT) | ||
, _ws("/ws") | ||
, _events("/events") | ||
{ | ||
} | ||
|
||
void WebApiClass::init() | ||
{ | ||
using namespace std::placeholders; | ||
|
||
_server.addHandler(&_ws); | ||
_server.addHandler(&_events); | ||
|
||
_ws.onEvent(std::bind(&WebApiClass::onWebsocketEvent, this, _1, _2, _3, _4, _5, _6)); | ||
|
||
_server.serveStatic("/", LITTLEFS, "/", "max-age=86400").setDefaultFile("index.htm"); | ||
_server.onNotFound(std::bind(&WebApiClass::onNotFound, this, _1)); | ||
_server.begin(); | ||
} | ||
|
||
void WebApiClass::onNotFound(AsyncWebServerRequest* request) | ||
{ | ||
// Handle Unknown Request | ||
request->send(404, "text/plain", "404 Not Found"); | ||
} | ||
|
||
void WebApiClass::onWebsocketEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len) | ||
{ | ||
if (type == WS_EVT_CONNECT) { | ||
char str[64]; | ||
sprintf(str, "Websocket: [%s][%u] connect", server->url(), client->id()); | ||
Serial.println(str); | ||
} else if (type == WS_EVT_DISCONNECT) { | ||
char str[64]; | ||
sprintf(str, "Websocket: [%s][%u] disconnect", server->url(), client->id()); | ||
Serial.println(str); | ||
} | ||
} | ||
|
||
WebApiClass WebApi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters