mrDarker
2025-07-16 1dbe46cd9d0f181d08d5a69f72d8548628a13b9d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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;
//};
 
#endif // WEBSOCKETCLIENT_H