qhttp icon indicating copy to clipboard operation
qhttp copied to clipboard

Can't start the QHttpServer outside of main ?

Open dmacattack opened this issue 8 years ago • 2 comments

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?

dmacattack avatar Jan 26 '18 15:01 dmacattack

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.

ghost avatar Mar 17 '18 05:03 ghost

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

dmacattack avatar Mar 19 '18 16:03 dmacattack