示例:魔术8球

Magic 8 Ball

魔术8球

Creating a HTTP2 connection between a Qt GRPC client and a C++ gRPC server.

在Qt GRPC客户端和C++GRPC服务器之间创建HTTP2连接。

Magic 8 ball shows an answer it receives from a server:

魔术8球显示了它从服务器收到的回复:

Magic 8 ball has the following components:

魔术8号球由以下部件组成:

  • magic8ball Qt GRPC client application that includes the qt_add_protobuf() and qt_add_grpc() CMake functions for message and service Qt code generation.
  • ​magic8ball Qt GRPC客户端应用程序,包括用于生成消息和服务Qt代码的qt_add_protobuf()和qt_add_grpc()CMake函数。
  • SimpleGrpcServer application that calls C++ gRPC plugin for generating server code and implementing simple server logic.
  • SimpleGrpcServer应用程序,它调用C++gRPC插件来生成服务器代码并实现简单的服务器逻辑。

Note: you need the C++ gRPC plugin installed. Find details here: Module prerequisites

注意:需要安装C++gRPC插件。在此处查找详细信息:模块先决条件

Both components use generated messages from the protobuf schema described in the exampleservice.proto file:

这两个组件都使用exampleservice.proto文件中描述的protobuf模式生成的消息:

syntax = "proto3";

package qtgrpc.examples;

message AnswerRequest {
  string message = 1;
}

message AnswerResponse {
  string message = 1;
}

service ExampleService {
  rpc answerMethod(AnswerRequest) returns (AnswerResponse) {}
}

The client application binds on the localhost with port 50051:

客户端应用程序通过端口50051绑定在本地主机上:

QGrpcChannelOptions channelOptions(QUrl("http://localhost:50051", QUrl::StrictMode));
m_client->attachChannel(std::make_shared<QGrpcHttp2Channel>(channelOptions));

And sends a request to the server part:

并向服务器部分发送请求:

void ClientService::sendRequest()
{
    // clean error on UI before new request
    emit errorRecieved("");

    AnswerRequest request;
    request.setMessage("sleep");

    m_client->answerMethod(request, m_response.get());
}

Click the Ask question button to send the request to the SimpleGrpcServer application.

单击“询问问题”按钮将请求发送到SimpleGrpcServer应用程序。

The SimpleGrpcServer application chooses a random answer from the list of answers and sends the data to the client's port.

SimpleGrpcServer应用程序从答案列表中选择一个随机答案,并将数据发送到客户端的端口。

Status ExampleServiceServiceImpl::answerMethod(grpc::ServerContext *,
                                               const AnswerRequest *request,
                                               AnswerResponse *response)
{
    if (request->message() == "sleep")
        QThread::msleep(2000);

    response->set_message(std::string(answers[generateRandomIndex()]));
    return Status();
}

After receiving a response the client application shows the answer.

在接收到响应之后,客户端应用程序显示回复。

Example project @ code.qt.io

© 2023 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值