EdgeInspector_App/WebSocket/WebSocketClient.h
@@ -1,129 +1,129 @@
#ifndef WEBSOCKETCLIENT_H
#define WEBSOCKETCLIENT_H
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include <string>
#include <vector>
#include <iostream>
#include <thread>
#include <atomic>
#include <mutex>
#include <functional>
#include <chrono>
typedef websocketpp::client<websocketpp::config::asio_client> client;
class WebSocketClient {
public:
    using message_ptr = websocketpp::config::asio_client::message_type::ptr;
    using connection_hdl = websocketpp::connection_hdl;
    using OpenHandler = std::function<void()>;
    using MessageHandler = std::function<void(const std::string&)>;
    using CloseHandler = std::function<void()>;
    using FailHandler = std::function<void()>;
   using PongHandler = std::function<void(const std::string&)>;
    WebSocketClient();
    ~WebSocketClient();
    // 连接到指定的URI
    void connect(const std::string& uri);
    // 增加客户端的状态标识
    bool is_busy() const;
    void set_busy(bool busy);
    // 发送文本消息
    void send(const std::string& message);
    // 发送二进制数据
    void send_binary(const std::vector<char>& binary_data);
   // 通过队列发送文本消息
    void enqueue_message(const std::string& message);
   // 通过队列发送二进制数据
    void enqueue_binary(const std::vector<char>& binary_data);
   // 处理下一个消息
    void process_next_message();
    void process_next_binary();
   // 发送文本消息并等待回复
    bool send_and_wait(const std::string& message, int timeout_ms);
   // 发送二进制数据并等待回复
    bool send_binary_and_wait(const std::vector<char>& binary_data, int timeout_ms);
    // 关闭连接
    void close();
    // 设置各种回调函数
    void set_open_handler(OpenHandler handler);
    void set_message_handler(MessageHandler handler);
    void set_close_handler(CloseHandler handler);
    void set_fail_handler(FailHandler handler);
    void set_pong_handler(PongHandler handler);
    // 设置心跳间隔(单位:秒)
    void set_heartbeat_interval(int interval);
    // 获取当前连接的URL
    std::string get_current_url();
    // 检查当前连接是否仍然有效
    bool is_connected() const;
private:
    // WebSocket 运行函数
    void run();
    // 心跳检查函数
    void heartbeat();
    // 回调函数触发器
    void on_open(websocketpp::connection_hdl hdl);
    void on_message(connection_hdl hdl, message_ptr msg);
    void on_close(websocketpp::connection_hdl hdl);
    void on_fail(websocketpp::connection_hdl hdl);
    void on_pong(websocketpp::connection_hdl hdl, const std::string& payload);
private:
    // WebSocket客户端实例
    client m_client;
    // WebSocket 连接句柄
    connection_hdl m_hdl;
    // 线程相关
    std::thread m_thread;
    std::thread m_heartbeat_thread;
    std::atomic<bool> m_connected;
    std::atomic<bool> m_reconnect;
    int m_heartbeat_interval;
    bool m_message_received;
    // 发送队列
    std::queue<std::string> m_message_queue;
    std::queue<std::vector<char>> m_binary_queue;
    // 标识客户端是否忙碌
    std::atomic<bool> m_busy;
    // 线程同步相关
    bool m_close_done;
    std::condition_variable m_cv;
    // 互斥锁,用于保护共享资源
    std::mutex m_mutex;
    // 回调函数
    OpenHandler m_open_handler;
    MessageHandler m_message_handler;
    CloseHandler m_close_handler;
    FailHandler m_fail_handler;
    PongHandler m_pong_handler;
};
//#include <websocketpp/config/asio_no_tls_client.hpp>
//#include <websocketpp/client.hpp>
//#include <string>
//#include <vector>
//#include <iostream>
//#include <thread>
//#include <atomic>
//#include <mutex>
//#include <functional>
//#include <chrono>
//
//typedef websocketpp::client<websocketpp::config::asio_client> client;
//
//class WebSocketClient {
//public:
//    using message_ptr = websocketpp::config::asio_client::message_type::ptr;
//    using connection_hdl = websocketpp::connection_hdl;
//    using OpenHandler = std::function<void()>;
//    using MessageHandler = std::function<void(const std::string&)>;
//    using CloseHandler = std::function<void()>;
//    using FailHandler = std::function<void()>;
//   using PongHandler = std::function<void(const std::string&)>;
//
//    WebSocketClient();
//    ~WebSocketClient();
//
//    // 连接到指定的URI
//    void connect(const std::string& uri);
//
//    // 增加客户端的状态标识
//    bool is_busy() const;
//    void set_busy(bool busy);
//
//    // 发送文本消息
//    void send(const std::string& message);
//
//    // 发送二进制数据
//    void send_binary(const std::vector<char>& binary_data);
//
//   // 通过队列发送文本消息
//    void enqueue_message(const std::string& message);
//
//   // 通过队列发送二进制数据
//    void enqueue_binary(const std::vector<char>& binary_data);
//
//   // 处理下一个消息
//    void process_next_message();
//    void process_next_binary();
//
//   // 发送文本消息并等待回复
//    bool send_and_wait(const std::string& message, int timeout_ms);
//
//   // 发送二进制数据并等待回复
//    bool send_binary_and_wait(const std::vector<char>& binary_data, int timeout_ms);
//
//    // 关闭连接
//    void close();
//
//    // 设置各种回调函数
//    void set_open_handler(OpenHandler handler);
//    void set_message_handler(MessageHandler handler);
//    void set_close_handler(CloseHandler handler);
//    void set_fail_handler(FailHandler handler);
//    void set_pong_handler(PongHandler handler);
//
//    // 设置心跳间隔(单位:秒)
//    void set_heartbeat_interval(int interval);
//
//    // 获取当前连接的URL
//    std::string get_current_url();
//
//    // 检查当前连接是否仍然有效
//    bool is_connected() const;
//
//private:
//    // WebSocket 运行函数
//    void run();
//
//    // 心跳检查函数
//    void heartbeat();
//
//    // 回调函数触发器
//    void on_open(websocketpp::connection_hdl hdl);
//    void on_message(connection_hdl hdl, message_ptr msg);
//    void on_close(websocketpp::connection_hdl hdl);
//    void on_fail(websocketpp::connection_hdl hdl);
//    void on_pong(websocketpp::connection_hdl hdl, const std::string& payload);
//
//private:
//    // WebSocket客户端实例
//    client m_client;
//
//    // WebSocket 连接句柄
//    connection_hdl m_hdl;
//
//    // 线程相关
//    std::thread m_thread;
//    std::thread m_heartbeat_thread;
//    std::atomic<bool> m_connected;
//    std::atomic<bool> m_reconnect;
//    int m_heartbeat_interval;
//    bool m_message_received;
//
//    // 发送队列
//    std::queue<std::string> m_message_queue;
//    std::queue<std::vector<char>> m_binary_queue;
//
//    // 标识客户端是否忙碌
//    std::atomic<bool> m_busy;
//
//    // 线程同步相关
//    bool m_close_done;
//    std::condition_variable m_cv;
//
//    // 互斥锁,用于保护共享资源
//    std::mutex m_mutex;
//
//    // 回调函数
//    OpenHandler m_open_handler;
//    MessageHandler m_message_handler;
//    CloseHandler m_close_handler;
//    FailHandler m_fail_handler;
//    PongHandler m_pong_handler;
//};
#endif // WEBSOCKETCLIENT_H