您的位置:首页 > 其它

tufao安装和实例详解

2017-01-12 20:58 351 查看
安装tufao

获取代码 github

编译和安装

sudo apt-get install cmake qtsdk

在tufao目录下创建build目录

cd build

cmake .. -DCMAKE_INSTALL_PREFIX=/usr

make

sudo make install

创建工程

创建空的工程

工程文件中增加
CONFIG += TUFAO1 C++11


增加一个类MyServer,一定是QObject派生类

增加一个main.cpp实现main函数

在MyServer的构造函数,创建Tufao::HttpServer对象server

将server的信号
requestReady
和自己写的槽函数
slotRequestReady
连接

slotRequestReady
函数中,实现http协议的响应报文。

> 步骤:

1) 在main.c

#include <QCoreApplication>
#include "MyServer.h"

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    new MyServer;

    return 0;
}

2) 在MyServer.h中添加: 

#include <Tufao/httpserver.h>     // 引入头文件

// create http server
Tufao::HttpServer *server;

MyServer.cpp

MyServer::MyServer(QObject *parent) : QObject(parent)
{
     // 创建一个服务器
    server = new Tufao::HttpServer;
     // 开启监听
    if (server->listen(QHostAddress::Any, 8080))
    {
        qDebug() << "listen ok at 8080";
    }
    else
    {
        qDebug() << "listen error at 8080";
    }
}

当有client端发送请求来的时候, tufao用的是信号和槽的机制。

3)当有请求来的时候要添加槽函数 

MyServer.h 

public slots:
    void slotQequestReady(Tufao::HttpServerRequest&,Tufao::HttpServerResponse&);

MyServer.cpp

// 连接
connect(server, SIGNAL(requestReady(Tufao::HttpServerRequest&,Tufao::HttpServerResponse&)),
            this, SLOT(slotQequestReady(Tufao::HttpServerRequest&,Tufao::HttpServerResponse&));

// 或者用函数是指针的方式
connect(server, &Tufao::HttpServer::requestReady, this, &MyServer::slotQequestReady);

槽函数编写:

void MyServer::slotQequestReady(Tufao::HttpServerRequest& request,Tufao::HttpServerResponse& response)
{
    //1. status line
    response.writeHead(Tufao::HttpResponseStatus::OK);

    //2. write content
    response.write("this my first tufao<br/>");
    response.write("hahahah  ");

    //3. tips: write end and send data 重要
    response.end("byebye tufao end<br/>");
}

然后访问主机加上端口名就可以访问了。

> url链接带请求, 例如:

`http://192.168.49.136?user=aa&password=bbb`

1) 当有链接请求的时候,可以在槽函数里来获取url传过来的东西:
```c++
//先导入头文件: include <QUrl>
QString url = request.url().toString();
// 响应报文
response.end(url.toUtf8());
```
然后会返回: `/?user=name&password=pass`

curl命令请求post数据, 用 -d:

curl "http://192.168.49.136:8081" -d "this is post data"

传输文件用:@+路径

curl "http://192.168.49.136:8081" -d "@test.txt"

上传文件时,如果文件过大,那么会有一个:
connect (&request, &Tufao::HttpServerRequest::ready, );

最后一次上传文件时:
connect (&request, &Tufao::HttpServerRequest::end, );

> post请求 案例

MyServerHandle.h

#ifndef MYSERVERHANDLEPOST_H
#define MYSERVERHANDLEPOST_H

#include <QObject>
#include <Tufao/HttpServer>
#include <Tufao/HttpServerRequest>
#include <Tufao/HttpServerResponse>

class MyServerHandlePost : public QObject
{
    Q_OBJECT
public:
    explicit MyServerHandlePost(QObject *parent = 0);

    Tufao::HttpServer *server;

    void handlePostData(Tufao::HttpServerRequest&,Tufao::HttpServerResponse&);

signals:

public slots:
    void slotRequestReady(Tufao::HttpServerRequest&,Tufao::HttpServerResponse&);

};

#endif // MYSERVERHANDLEPOST_H

MyServerHandle.cpp

#include "MyServerHandlePost.h"
#include <QFile>

MyServerHandlePost::MyServerHandlePost(QObject *parent) : QObject(parent)
{
    server = new Tufao::HttpServer;

    if(server->listen(QHostAddress::Any, 8081))
    {
        qDebug() << "listen ok at 8081";
    }
    else
    {
        qDebug() << "listen error at 8081";
    }

    connect(server, SIGNAL(requestReady(Tufao::HttpServerRequest&,Tufao::HttpServerResponse&)),
            this, SLOT(slotRequestReady(Tufao::HttpServerRequest&,Tufao::HttpServerResponse&)));
}

void MyServerHandlePost::handlePostData(Tufao::HttpServerRequest &request, Tufao::HttpServerResponse &response)
{
    QFile file("/home/xueguoliang/b.txt");
    file.open(QFile::WriteOnly);
    QByteArray data = request.readBody();
    file.write(data);
    file.close();
#if 0
    // read body
    qDebug() << request.readBody();
#endif

    response.writeHead(Tufao::HttpResponseStatus::OK);
    response.write("post data is write ok\n");
}

void MyServerHandlePost::slotRequestReady(Tufao::HttpServerRequest &request, Tufao::HttpServerResponse &response)
{
    if (request.method() == "POST")
    {
        // end signal: all data is ready   &: get all param
        connect(&request, &Tufao::HttpServerRequest::end, [&](){
            handlePostData(request, response);
        });
    }
    else
    {
        response.writeHead(Tufao::HttpResponseStatus::OK);
        response.end("i need post method\n");
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  tufao 通信库