61 lines
1.3 KiB
C++
61 lines
1.3 KiB
C++
#ifndef MEASUREMENTDATAUTILS_H
|
||
#define MEASUREMENTDATAUTILS_H
|
||
|
||
#include "networkCommon.h"
|
||
#include <QObject>
|
||
#include <QWebSocket>
|
||
/**
|
||
* @brief WebSocket数据客户端
|
||
*
|
||
* 负责与后台数据服务建立WebSocket连接,接收实时数据
|
||
*/
|
||
class QTimer;
|
||
class WebSocketClient : public QObject
|
||
{
|
||
Q_OBJECT
|
||
|
||
public:
|
||
explicit WebSocketClient(QObject* parent = nullptr);
|
||
~WebSocketClient();
|
||
|
||
//连接管理
|
||
bool connectToServer(const QUrl& url);
|
||
void disconnectFromServer();
|
||
//bool isConnected();
|
||
|
||
//数据配置
|
||
void setReconnectInterval(int intervalMs);
|
||
void setMaxReconnectAttempts(int maxAttempts);
|
||
|
||
signals:
|
||
void dataReceived(const QString& dataMsg);
|
||
|
||
private slots:
|
||
void onConnected();
|
||
void onDisconnected();
|
||
void onError(QAbstractSocket::SocketError error);
|
||
void onTextMessageReceived(const QString& message);
|
||
void onReconnectTimeout();
|
||
void resetReconnect();
|
||
|
||
private:
|
||
void setupWebSocket();
|
||
void cleanupWebSocket();
|
||
void scheduleReconnect();
|
||
|
||
QWebSocket* m_webSocket;
|
||
QUrl m_serverUrl;
|
||
|
||
//重连相关
|
||
QTimer* m_reconnectTimer;
|
||
int m_reconnectInterval;
|
||
int m_reconnectAttempts;
|
||
int m_maxReconnectAttempts;
|
||
|
||
// 状态
|
||
bool m_connected;
|
||
ConnectionStatus m_connectionStatus;
|
||
};
|
||
|
||
#endif
|