Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpServer接收回调问题 #26

Closed
zlozl233 opened this issue Jul 27, 2020 · 4 comments
Closed

HttpServer接收回调问题 #26

zlozl233 opened this issue Jul 27, 2020 · 4 comments

Comments

@zlozl233
Copy link

您好,还是原来那个问题,我在linux(Ubuntu18.04)qt(5.14.1)下也测试过。使用lambda函数,好像只能在mian函数内,客户端请求才能收到,一但在不在,客户端请求接收不到,返回false。还请帮忙看看 int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.setWindowTitle("HttpServer");
w.show();
#if 1
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() ) ) );
// 注1:因为一个session对应一个单一的HTTP请求,所以session只能reply一次
// 注2:在reply后,session的生命周期不可控,所以reply后不要再调用session的接口了
} );
qDebug() << "listen:" << tcpServerManage.listen( QHostAddress("192.168.124.128"), 9091);
qDebug() << ("Http服务已启动");
#endif
return a.exec();`

image
image
image

@188080501
Copy link
Owner

188080501 commented Jul 27, 2020

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

@zlozl233
Copy link
Author

`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事件来开启服务,然后客户端发送请求,服务端收不到请求。

@188080501
Copy link
Owner

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

@188080501
Copy link
Owner

188080501 commented Jul 28, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants