qhttp
qhttp copied to clipboard
Can't start the QHttpServer outside of main ?
I've been playing with this for too long. Why can't I start the QHttpServer outside the main? Such as in a class? I've tried passing in the app variable to the class too and no go.
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
MyClass *pClass = new MyClass();
pClass->startHttpServer(&app);
...
}
What is happening here?
Do you have find a solution? I'm facing the same issue too. The server seems to listen (i.e. isListening == true) but it doesn't actually answer to incoming connections.
It's quite odd to leave the code in the main.
I ended up doing this, its not what i wanted, but it works
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
// create http server instance
qhttp::server::QHttpServer httpServer(&app);
// create your class & pass in the object reference
MyClass *pClass = new MyClass();
pClass->start(&httpServer);
...
}
void MyClass::start(qhttp::server::QHttpServer *server)
{
server->listen( PORT, [&](qhttp::server::QHttpRequest *req, qhttp::server::QHttpResponse *res)
{
// lambda ... handle as you see fit
} );
}
I hope this helps