-
Notifications
You must be signed in to change notification settings - Fork 159
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
Comments
传的代码是在main里面能用的版本吧,不能用的代码也贴下 |
`void Widget::startHttpServer()
// session->replyJsonObject( { { { "message", "ok" } } } );
} |
C++语法基础不过关,用了局部变量,却期待有全局的效果 |
最简单修改就是改成new的 |
您好,还是原来那个问题,我在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();`
The text was updated successfully, but these errors were encountered: