-
Notifications
You must be signed in to change notification settings - Fork 137
Open
Labels
Description
I've a situation where the method for handling a particular HTTP request is inside a class and I'm unable to work out how to registerit with the HTTP server. Here's a minimal self-contained example of my issue:
#include <HTTPServer.hpp>
#include <HTTPRequest.hpp>
#include <HTTPResponse.hpp>
using namespace httpsserver;
class ConfigurationModule {
public:
void handleDisplayConfig(HTTPRequest *req, HTTPResponse *res) {
}
void init(HTTPServer server) {
std::function<void(HTTPRequest *, HTTPResponse *)> handler = std::bind(&ConfigurationModule::handleDisplayConfig, this, std::placeholders::_1, std::placeholders::_2);
server.registerNode(new ResourceNode("/configure", "GET", handler));
}
};
HTTPServer webserver = HTTPServer();
ConfigurationModule config = ConfigurationModule();
void handler1(HTTPRequest *req, HTTPResponse *res) {
}
void handler2(HTTPRequest *req, HTTPResponse *res) {
}
void setup() {
Serial.begin(115200);
webserver.registerNode(new ResourceNode("/htmlResponse", "GET", handler1));
webserver.registerNode(new ResourceNode("/jsonResponse", "GET", handler2));
config.init(webserver);
webserver.start();
}
void loop() {
webserver.loop();
}
This is a technique I've used across my fairly extensive codebase (which I'm currently porting from the ESP8266). However, the code in the init
function won't compile, specifically it complains that handler
has an incorrect signature, even though the method in question has the same signature as handler1 and handler2:
no known conversion for argument 3 from 'std::function<void(httpsserver::HTTPRequest*, httpsserver::HTTPResponse*)>' to 'void (*)(httpsserver::HTTPRequest*, httpsserver::HTTPResponse*)'
I'm unable to work out the correct syntax for resolving the class method to the typedef HTTPSCallbackFunction, or how to tweak the existing code to get rid of the error I have.
Can anybody help me here?
Metadata
Metadata
Assignees
Labels
Projects
Milestone
Relationships
Development
Select code repository
Activity
fhessel commentedon Jun 6, 2020
Sorry it took so long to get back to this.
That indeed wasn't possible by now, as you could only use classic function pointers with the
ResourceNode
constructor, which doesn't work with the outcome of anstd::bind
.I created PR #91 which should now allow to use both, being backward-compatible. That still needs a bit of testing before I can safely merge into the master branch, but maybe it helps you to proceed.
This slightly modified version of your example should work with the PR:
I hope it's not too late for you, however, that issue has been on my todo list for some time now anyway. It would be nice if you could give me feedback in either case.
Thomas-Blondeau commentedon Jun 24, 2020
Hello !
I got the same issue this week and I am happy to read that you just prepared a PR. Is it possible to use the fix right now ? Maybe if I pull the branch ?
Thank you !
fhessel commentedon Jun 24, 2020
Hi,
you can clone the repository and switch to the feature branch:
If you're using Platform IO, do it in your project's
lib
folder, if you use the Arduino IDE, do it in thelibraries
folder in your Sketchbook.It would be great if you could tell me if that works for you, or if you run into problems. If it works, I'd merge the PR soon.
Thomas-Blondeau commentedon Jun 24, 2020
I will clone the branch tonight and test it. I use Visual code with the Arduino Plugin.
I'll give you my feedbacks very soon.
surfsoft commentedon Oct 13, 2020
Apologies for my much delayed response, I hope to get back to this in the next week or two...