forked from me-no-dev/ESPAsyncWebServer
Adding example of global & class functions for request handler (#107)
* Adding example of global & class functions for request handler * Make title the same as the one in the table of contents
This commit is contained in:
52
README.md
52
README.md
@@ -63,6 +63,7 @@ To use this library you might need to have the latest git versions of [ESP8266](
|
|||||||
- [Scanning for available WiFi Networks](#scanning-for-available-wifi-networks)
|
- [Scanning for available WiFi Networks](#scanning-for-available-wifi-networks)
|
||||||
- [Remove handlers and rewrites](#remove-handlers-and-rewrites)
|
- [Remove handlers and rewrites](#remove-handlers-and-rewrites)
|
||||||
- [Setting up the server](#setting-up-the-server)
|
- [Setting up the server](#setting-up-the-server)
|
||||||
|
- [Setup global and class functions as request handlers](#setup-global-and-class-functions-as-request-handlers)
|
||||||
- [Methods for controlling websocket connections](#methods-for-controlling-websocket-connections)
|
- [Methods for controlling websocket connections](#methods-for-controlling-websocket-connections)
|
||||||
|
|
||||||
## Why should you care
|
## Why should you care
|
||||||
@@ -1020,6 +1021,57 @@ void loop(){
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Setup global and class functions as request handlers
|
||||||
|
|
||||||
|
```arduino
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include <ESPAsyncWebserver.h>
|
||||||
|
#include <Hash.h>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
void handleRequest(AsyncWebServerRequest *request)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
class WebClass
|
||||||
|
{
|
||||||
|
public :
|
||||||
|
WebClass(){
|
||||||
|
};
|
||||||
|
|
||||||
|
AsyncWebServer classWebServer = AsyncWebServer(80);
|
||||||
|
|
||||||
|
void classRequest (AsyncWebServerRequest *request)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void begin(){
|
||||||
|
|
||||||
|
// attach global request handler
|
||||||
|
classWebServer.on("/example", HTTP_ANY, handleRequest);
|
||||||
|
|
||||||
|
// attach class request handler
|
||||||
|
classWebServer.on("/example", HTTP_ANY, std::bind(&WebClass::classRequest, this, std::placeholders::_1));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
AsyncWebServer globalWebServer(80);
|
||||||
|
WebClass webClassInstance;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
|
||||||
|
// attach global request handler
|
||||||
|
globalWebServer.on("/example", HTTP_ANY, handleRequest);
|
||||||
|
|
||||||
|
// attach class request handler
|
||||||
|
globalWebServer.on("/example", HTTP_ANY, std::bind(&WebClass::classRequest, webClassInstance, std::placeholders::_1));
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### Methods for controlling websocket connections
|
### Methods for controlling websocket connections
|
||||||
|
|
||||||
```arduino
|
```arduino
|
||||||
|
Reference in New Issue
Block a user