客户端开发

更新时间:
2024-02-20
下载文档

客户端开发

本节介绍 QVSOA 客户端使用 Datagram 的方法。

开发须知

QVSOA 使用 QVsoaClient::sendDatagram 方法发送数据包。

开发示例

下面以向服务端发送数据包并接收来自服务端的数据包为例,介绍使用 Datagram 开发 VSOA 客户端的方法。

#include <QCoreApplication>
#include <QDebug>
#include <QVsoa>

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 onDatagram(QVsoaClient *client, QString url, QVsoaPayload payload)
{
    qDebug() << "Datagram received from URL" << url << "with payload:" << payload.param();
    client->sendDatagram(url, payload);
}

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::datagram, std::bind(onDatagram, &client, _1, _2));

    // Connect to server with password
    client.connect2server("vsoa://axis_server", {}, 1000);
    // Enable automatic connections
    client.autoConnect(1000, 500);
    // Subscribe /axis
    client.subscribe("/axis");
    // Enable data consistency on /axis
    client.autoConsistent({"/axis"}, 1000);

    return a.exec();
}
文档内容是否对您有所帮助?
有帮助
没帮助