웹소켓 MQTT 구독

MQTT 클라이언트와 웹 소켓 연결을 결합합니다.

웹소켓 MQTT 구독은 웹 소켓 연결을 QMqttClient 과 결합하기 위해 사용자 지정 QIODevice 을 디자인하는 방법을 보여줍니다.

사용자 지정 QIODevice 만들기

새로운 사용자 정의 장치인 WebSocketIODeviceQIODevice 의 서브클래스여야 합니다:

class WebSocketIODevice : public QIODevice
{
    Q_OBJECT
public:
    WebSocketIODevice(QObject *parent = nullptr);

    bool isSequential() const override;
    qint64 bytesAvailable() const override;

    bool open(OpenMode mode) override;
    void close() override;

    qint64 readData(char *data, qint64 maxlen) override;
    qint64 writeData(const char *data, qint64 len) override;

    void setUrl(const QUrl &url);
    void setProtocol(const QByteArray &data);
Q_SIGNALS:
    void socketConnected();

public slots:
    void handleBinaryMessage(const QByteArray &msg);
    void onSocketConnected();

private:
    QByteArray m_protocol;
    QByteArray m_buffer;
    QWebSocket m_socket;
    QUrl m_url;
};

연결 및 구독을 관리할 클래스 디자인하기

WebSocketIODeviceQMqttClientQMqttSubscription 과 함께 ClientSubscription 클래스의 비공개 멤버가 됩니다:

private:
    QMqttClient m_client;
    QMqttSubscription *m_subscription;
    QUrl m_url;
    QString m_topic;
    WebSocketIODevice m_device;
    int m_version;

메시지 구독 및 수신

주요 로직은 ClientSubscription 클래스의 connectAndSubscribe() 메서드에서 구현됩니다. 웹 소켓이 성공적으로 연결되었는지 확인해야 웹 소켓을 통해 MQTT 연결을 초기화할 수 있습니다. MQTT 연결이 설정되면 QMqttClient 에서 토픽을 구독할 수 있습니다. 구독이 성공하면 QMqttSubscription 을 사용하여 ClientSubscription 클래스의 handleMessage() 메서드가 처리할 구독된 토픽의 메시지를 수신할 수 있습니다.

void ClientSubscription::connectAndSubscribe() { qCDebug(lcWebSocketMqtt)<< "브로커에 연결 중 "<< m_url; m_device.setUrl(m_url); m_device.setProtocol(m_version== 3 ? "mqttv3.1": "mqtt"); connect(&m_device, &WebSocketIODevice::socketConnected, this, [this]() { qCDebug(lcWebSocketMqtt)<< "WebSocket 연결, MQTT 연결 초기화 중입니다."; m_client.setProtocolVersion(m_version== 3 ? QMqttClient::MQTT_3_1 : QMqttClient::MQTT_3_1_1); m_client.setTransport(&m_device, QMqttClient::IODevice); connect(&m_client, &QMqttClient::connected, this, [this]() { qCDebug(lcWebSocketMqtt)<< "MQTT 연결이 설정되었습니다"; m_subscription = m_client.subscribe(m_topic); if (!m_subscription) {                qDebug() << "Failed to subscribe to " << m_topic;
               emit errorOccured(); } connect(m_subscription, &.QMqttSubscription::stateChanged, [](QMqttSubscription::SubscriptionState s) { qCDebug(lcWebSocketMqtt)<< "구독 상태 변경됨:"<< s; }); connect(m_subscription, &QMqttSubscription::messageReceived, [this](QMqttMessage msg) { handleMessage(msg.payload()); }); }); m_client.connectToHost(); }); if (!m_device.open(QIODevice::ReadWrite))        qDebug() << "Could not open socket device";

파일:

© 2025 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.