GithubHelp home page GithubHelp logo

Comments (4)

188080501 avatar 188080501 commented on July 23, 2024

传的代码是在main里面能用的版本吧,不能用的代码也贴下

from jqhttpserver.

zlozl233 avatar zlozl233 commented on July 23, 2024

`void Widget::startHttpServer()
{
JQHttpServer::TcpServerManage tcpServerManage(10); // 设置最大处理线程数,默认2个
tcpServerManage.setHttpAcceptedCallback(
[]( const QPointer< JQHttpServer::Session > &session )
{
// 回调发生在新的线程内,不是主线程,请注意线程安全
// 若阻塞了此回调,那么新的连接将不会得到处理(默认情况下有2个线程可以阻塞2次,第3个连接将不会被处理)
qDebug() << "请求来源IP: "+session->requestSourceIp(); //请求来源IP
qDebug() << "url:"+session->requestUrl(); //url 后面参数
qDebug() << "请求方式: "+session->requestMethod(); //POST GET
qDebug() << "postdata: "+session->requestBody();
QByteArray postdata = session->requestBody();
QString ssize = QString::number(postdata.size(),10);
qDebug() << "postdata.size: "+ssize;

    session->replyText( QString( "url:%1\nbody:%2" ).arg( session->requestUrl(), QString( session->requestBody() ) ) );
    //        session->replyRedirects( QUrl( "http://www.baidu.com" ) );

// session->replyJsonObject( { { { "message", "ok" } } } );
// session->replyJsonArray( { "a", "b", "c" } );
// if (session->requestUrl() == "/download/qt-creator-opensource-windows-x86_64-4.4.1.exe") {
// session->replyFile("E:\QT5.14\qt-creator-opensource-windows-x86_64-4.4.1.exe");
// }

    // 注1:因为一个session对应一个单一的HTTP请求,所以session只能reply一次
    // 注2:在reply后,session的生命周期不可控,所以reply后不要再调用session的接口了
} );
qDebug() << "listen:" << tcpServerManage.listen( QHostAddress("192.168.124.128"), 9091);
qDebug() << ("Http服务已启动");

}
`
是的,我在main函数里能用,如上,我定义一个函数来开启服务,用一个button事件来开启服务,然后客户端发送请求,服务端收不到请求。

from jqhttpserver.

188080501 avatar 188080501 commented on July 23, 2024

C++语法基础不过关,用了局部变量,却期待有全局的效果

from jqhttpserver.

188080501 avatar 188080501 commented on July 23, 2024

最简单修改就是改成new的
auto tcpServerManage = new JQHttpServer::TcpServerManage(2);

from jqhttpserver.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.