/** * @file WsClient.h * @brief Thin QWebSocket wrapper with auto-reconnect. */ #pragma once #include #include #include #include #include #include namespace shq { class WsClient : public QObject { Q_OBJECT public: explicit WsClient(QObject* parent = nullptr); /** (Re)connect to host:port; starts the auto-reconnect loop. */ void connectTo(const QString& host, uint16_t port); /** Switch target and reconnect immediately. */ void reconnectTo(const QString& host, uint16_t port); void close(); bool isConnected() const { return connected_; } QString host() const { return host_; } uint16_t port() const { return port_; } public Q_SLOTS: void sendText(const QString& json); void sendText(const std::string& json) { sendText(QString::fromStdString(json)); } Q_SIGNALS: void connectedChanged(bool connected); void textReceived(const QString& json); void binaryReceived(const QByteArray& data); private Q_SLOTS: void onConnected(); void onDisconnected(); void onTick(); private: void openSocket(); QWebSocket socket_; QTimer reconnectTimer_; QString host_; uint16_t port_ = 0; bool connected_ = false; bool wantOpen_ = false; }; } /* namespace shq */