当前位置: 首页 > news >正文

Qt6.8 GRPC功能使用(2)标准 Qt实现客户端

简介

基于之前的文章所说, Qt6.7之后才开始支持客户端、服务端、及双向流,恰好电脑需要重装,看到Qt6.8版本就直接安装了,内容也是使用Qt6.8的版本进行编译的

客户端实现步骤

1. 安装Qt6.8, 包含GRPC功能模块

Qt 6.8安装目录下包含这两个组件就可以将.proto生成Qt库支持了

在这里插入图片描述

2. 基于上一篇的示例,生成Qt支持

基于 Qt6.8 GRPC功能使用(1)标准GRPC C++ exmple编译环境搭建
新建一个Qt grpc客户端应用, 将 helloworld.proto 拷贝到该应用中,.pro加上grpc支持 .pro += grpc

proto文件

.proto文件内容, 来自xxx\grpc-1.55.0\examples\protos\helloworld.proto

syntax = "proto3";option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";package helloworld;// The greeting service definition.
service Greeter {// Sends a greetingrpc SayHello (HelloRequest) returns (HelloReply) {}rpc SayHelloStreamReply (HelloRequest) returns (stream HelloReply) {}
}// The request message containing the user's name.
message HelloRequest {string name = 1;
}// The response message containing the greetings
message HelloReply {string message = 1;
}
生成 Proto buffer的Qt支持

D:\Softwares\Paths\msys64\mingw64\bin\protoc.exe --plugin=protoc-gen-qtprotobuf=D:\Softwares\IDEs\Qt\6.8.0\mingw_64\bin\qtprotobufgen.exe -I E:/Workspace/Qt/greeter_client_qt/grpc --qtprotobuf_out=“E:/Workspace/Qt/greeter_client_qt/grpc” “E:/Workspace/Qt/greeter_client_qt/grpc/helloworld.proto”

生成 GRPC 的Qt支持

D:\Softwares\Paths\msys64\mingw64\bin\protoc.exe --plugin=protoc-gen-qtgrpc=D:\Softwares\IDEs\Qt\6.8.0\mingw_64\bin\qtgrpcgen.exe -I E:/Workspace/Qt/greeter_client_qt/grpc --qtgrpc_out=“E:/Workspace/Qt/greeter_client_qt/grpc” “E:/Workspace/Qt/greeter_client_qt/grpc/helloworld.proto”

在这里插入图片描述

3. 增加client代码

#include <QCoreApplication>
#include <QGrpcChannelOptions>
#include <QGrpcServerStream>
#include "helloworld_client.grpc.qpb.h"
#include <QGrpcHttp2Channel>
#include <QDebug>int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);helloworld::Greeter::Client client;QAbstractSocket::connect(&client, &QAbstractGrpcClient::errorOccurred, [=](const QGrpcStatus &status){qDebug().noquote() << "errorOccurred : " << status.code() << status.message();});QAbstractSocket::connect(&client, &QAbstractGrpcClient::channelChanged, [=](){qDebug().noquote() << "channelChanged!";});QUrl url ("http://localhost:50051");QGrpcChannelOptions channelOptions(url);std::shared_ptr<QAbstractGrpcChannel> channel = std::make_shared<QGrpcHttp2Channel>(channelOptions);client.attachChannel(channel);helloworld::HelloRequest req;req.setName("GrayHsu");std::shared_ptr<QGrpcCallReply> grpcReply = client.SayHello(req);QAbstractSocket::connect(grpcReply.get(), &QGrpcCallReply::errorOccurred, [=](const QGrpcStatus &status){qDebug().noquote() << "sayHello errorOccurred : " << status.code() << status.message();});QAbstractSocket::connect(grpcReply.get(), &QGrpcCallReply::finished, [=](){helloworld::HelloReply rep;grpcReply->read(&rep);qDebug().noquote() << "finished: " << rep.message();});return a.exec();
}

执行结果:
Server 没开
在这里插入图片描述
Server开了
在这里插入图片描述

4. Qt库简单说明

QGrpcCallReply用于接收返回信息, 提供了两个信号, 用于监控是否出错及是否结束(流时则为关闭)
std::shared_ptr grpcReply = client.SayHello(req);
在这里插入图片描述

Note

.proto 文件生成protobuffer和支持grpc语言文件支持指令

例如:

如下指令直接生成grpc_out的grpc支持,-cpp_out输出protobuffer支持D:\Softwares\Paths\msys64\mingw64\bin\protoc.exe --grpc_out E:/test/grpc-1.55.0/examples/cpp/helloworld/cmake/build --cpp_out E:/test/grpc-1.55.0/examples/cpp/helloworld/cmake/build -I E:/test/grpc-1.55.0/examples/protos --plugin=protoc-gen-grpc="D:/Softwares/Paths/msys64/mingw64/bin/grpc_cpp_plugin.exe" E:/test/grpc-1.55.0/examples/protos/helloworld.proto

相关文章:

  • ant 布局组件 组件等高设置
  • Docker日常使用记录
  • c++学习:构造函数
  • 【Micropython教程】点亮第一个LED与流水灯
  • 单细胞Seurat - 细胞聚类(3)
  • RK3568 android11 调试陀螺仪模块 MPU-6500
  • GPT 的基础 - T(Transformer)
  • spring boot 整合 minio存储 【使用篇】
  • kali linux通过aircrack-ng命令破解wifi密码
  • 【Linux】云服务器的Redis被黑
  • 【文献管理】zotero插件4——获取知网pdf、中文文献识别与目录生成
  • 【React 报错】—Remove untracked files, stash or commit any changes, and try again.
  • PostgreSQL常用SQL语句
  • android ROM编译之--repo使用指南
  • 为什么会对猫毛过敏?如何缓解?浮毛克星—宠物空气净化器推荐
  • (ckeditor+ckfinder用法)Jquery,js获取ckeditor值
  • __proto__ 和 prototype的关系
  • 230. Kth Smallest Element in a BST
  • Brief introduction of how to 'Call, Apply and Bind'
  • IDEA常用插件整理
  • java B2B2C 源码多租户电子商城系统-Kafka基本使用介绍
  • Laravel Telescope:优雅的应用调试工具
  • leetcode386. Lexicographical Numbers
  • MYSQL 的 IF 函数
  • node和express搭建代理服务器(源码)
  • thinkphp5.1 easywechat4 微信第三方开放平台
  • vue从入门到进阶:计算属性computed与侦听器watch(三)
  • 大快搜索数据爬虫技术实例安装教学篇
  • 第13期 DApp 榜单 :来,吃我这波安利
  • 聚类分析——Kmeans
  • 浏览器缓存机制分析
  • 面试遇到的一些题
  • 深入体验bash on windows,在windows上搭建原生的linux开发环境,酷!
  • 微信开放平台全网发布【失败】的几点排查方法
  • # 再次尝试 连接失败_无线WiFi无法连接到网络怎么办【解决方法】
  • #宝哥教你#查看jquery绑定的事件函数
  • #在 README.md 中生成项目目录结构
  • $GOPATH/go.mod exists but should not goland
  • (pt可视化)利用torch的make_grid进行张量可视化
  • (zt)最盛行的警世狂言(爆笑)
  • (笔试题)分解质因式
  • (超详细)2-YOLOV5改进-添加SimAM注意力机制
  • (附源码)springboot助农电商系统 毕业设计 081919
  • (紀錄)[ASP.NET MVC][jQuery]-2 純手工打造屬於自己的 jQuery GridView (含完整程式碼下載)...
  • (三)终结任务
  • (算法)Travel Information Center
  • (一)pytest自动化测试框架之生成测试报告(mac系统)
  • (一)插入排序
  • .bat批处理(六):替换字符串中匹配的子串
  • .NET Core WebAPI中封装Swagger配置
  • .NET 同步与异步 之 原子操作和自旋锁(Interlocked、SpinLock)(九)
  • .NET中的Exception处理(C#)
  • [ vulhub漏洞复现篇 ] JBOSS AS 4.x以下反序列化远程代码执行漏洞CVE-2017-7504
  • [ 第一章] JavaScript 简史
  • [4.9福建四校联考]