客户端开发
本节介绍 QVSOA 客户端使用 RPC 的方法。
RPC 调用参数
与定义函数时习惯区分函数的功能为“设置/获取”或“输入/输出”的概念一样,在 QVSOA 的 RPC 中也可以区分 "GET/SET" 操作。在 Qt 中使用 RPCMethod::GET
和 RPCMethod::SET
进行区分。
异步 RPC
QVSOA 提供了 QVsoaClientRPCInvoker
发起异步 RPC 的请求。
#include <QCoreApplication>
#include <QDebug>
#include <QVsoa>
constexpr char SERVER_PASSWORD[] = "123456";
using namespace std::placeholders;
void onConnected(bool ok, QString info)
{
if (!ok) {
qDebug() << "Connected with server failed!";
return;
}
qDebug() << "Connected with server:" << info;
}
void onDisconnected()
{
qDebug() << "Connection break";
}
void onReplay(QVsoaClientRPCInvoker *invoker, const QVsoaHeader header, const QVsoaPayload payload)
{
if (header.status() == StatusCode::SUCCESS) {
qDebug() << "Command /light reply:" << payload.param();
} else {
qDebug() << "Command /light error!";
}
invoker->deleteLater();
}
void lightCall(QVsoaClient *client, bool ok, QString)
{
if (!ok) {
return;
}
auto invoker = new QVsoaClientRPCInvoker(client, "/light", RPCMethod::GET);
QObject::connect(invoker, &QVsoaClientRPCInvoker::serverReply, std::bind(onReplay, invoker, _1, _2));
invoker->call(QVsoaPayload{});
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// Initilize client
QVsoaClient client;
if (client.isInvalid()) {
qDebug() << "Can not create VSOA client!";
return -1;
}
QObject::connect(&client, &QVsoaClient::connected, onConnected);
QObject::connect(&client, &QVsoaClient::disconnected, onDisconnected);
QObject::connect(&client, &QVsoaClient::connected, std::bind(lightCall, &client, _1, _2));
// Connect to server with password
client.connect2server("vsoa://light_server", SERVER_PASSWORD);
// Enable automatic connections
client.autoConnect(1000, 500);
return a.exec();
}
同步 RPC
QVSOA 提供了 QVsoaClientSynchronizer
发起同步 RPC 的请求。
#include <QCoreApplication>
#include <QDebug>
#include <QVsoa>
constexpr char SERVER_PASSWORD[] = "123456";
using namespace std::placeholders;
void onConnected(bool ok, QString info)
{
if (!ok) {
qDebug() << "Connected with server failed!";
return;
}
qDebug() << "Connected with server:" << info;
}
void onDisconnected()
{
qDebug() << "Connection break";
}
void lightCall(QVsoaClient *client, bool ok, QString)
{
if (!ok) {
return;
}
QVsoaClientSynchronizer sync(client);
auto [success, header] = sync.call("/light", RPCMethod::GET, QVsoaPayload{});
if (success) {
qDebug() << "Server /light reply:" << header.payload().param();
} else {
qDebug() << "Synchronous RPC call error!";
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// Initilize client
QVsoaClient client;
if (client.isInvalid()) {
qDebug() << "Can not create VSOA client!";
return -1;
}
QObject::connect(&client, &QVsoaClient::connected, onConnected);
QObject::connect(&client, &QVsoaClient::disconnected, onDisconnected);
QObject::connect(&client, &QVsoaClient::connected, std::bind(lightCall, &client, _1, _2));
// Connect to server with password
client.connect2server("vsoa://light_server", SERVER_PASSWORD);
// Enable automatic connections
client.autoConnect(1000, 500);
return a.exec();
}