From 1d66bbfc771aa8cf6b436176cfaa47b3a2c4862d Mon Sep 17 00:00:00 2001 From: hreintke Date: Wed, 14 Dec 2016 02:34:21 +0100 Subject: [PATCH] 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 --- README.md | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/README.md b/README.md index 420b7d8..72144d7 100644 --- a/README.md +++ b/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) - [Remove handlers and rewrites](#remove-handlers-and-rewrites) - [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) ## Why should you care @@ -1020,6 +1021,57 @@ void loop(){ } ``` +### Setup global and class functions as request handlers + +```arduino +#include +#include +#include +#include + +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 ```arduino