mrDarker
2025-08-22 f8ad0695ff2431cb90640be52b523d6434bdbf83
EdgeInspector_App/WebSocket/WebSocketClient.cpp
@@ -1,13 +1,13 @@
#include "stdafx.h"
#include "stdafx.h"
#include "WebSocketClient.h"
//WebSocketClient::WebSocketClient() : m_connected(false), m_reconnect(true), m_close_done(false), m_message_received(false), m_heartbeat_interval(5) {
//    // 清除之前的客户端配置并重新初始化
//    // 清除之前的客户端配置并重新初始化
//    m_client.clear_access_channels(websocketpp::log::alevel::all);
//    m_client.clear_error_channels(websocketpp::log::elevel::all);
//    m_client.init_asio();
//
//    // 设置事件回调
//    // 设置事件回调
//    m_client.set_open_handler([this](websocketpp::connection_hdl hdl) { on_open(hdl); });
//    m_client.set_message_handler([this](websocketpp::connection_hdl hdl, message_ptr msg) { on_message(hdl, msg); });
//    m_client.set_close_handler([this](websocketpp::connection_hdl hdl) { on_close(hdl); });
@@ -27,7 +27,7 @@
//        m_client.clear_access_channels(websocketpp::log::alevel::all);
//        m_client.clear_error_channels(websocketpp::log::elevel::all);
//
//        std::lock_guard<std::mutex> lock(m_mutex);  // 保护共享资源
//        std::lock_guard<std::mutex> lock(m_mutex);  // 保护共享资源
//        auto con = m_client.get_connection(uri, ec);
//
//        if (ec) {
@@ -39,7 +39,7 @@
//        m_client.connect(con);
//    }
//
//    // 启动工作线程
//    // 启动工作线程
//    m_thread = std::thread(&WebSocketClient::run, this);
//}
//
@@ -95,9 +95,9 @@
//        std::string message = m_message_queue.front();
//        m_message_queue.pop();
//
//        send(message); // 发送消息
//        send(message); // 发送消息
//
//        set_busy(false); // 设置为空闲状态,并处理下一个
//        set_busy(false); // 设置为空闲状态,并处理下一个
//        process_next_message();
//    }
//}
@@ -108,9 +108,9 @@
//        std::vector<char> binary_data = m_binary_queue.front();
//        m_binary_queue.pop();
//
//        send_binary(binary_data); // 发送二进制数据
//        send_binary(binary_data); // 发送二进制数据
//
//        set_busy(false); // 设置为空闲状态,并处理下一个
//        set_busy(false); // 设置为空闲状态,并处理下一个
//        process_next_binary();
//    }
//}
@@ -120,7 +120,7 @@
//    m_message_received = false;
//
//    if (m_connected.load()) {
//        // 发送消息
//        // 发送消息
//        websocketpp::lib::error_code ec;
//        m_client.send(m_hdl, message, websocketpp::frame::opcode::text, ec);
//        if (ec) {
@@ -128,7 +128,7 @@
//            return false;
//        }
//
//        // 等待消息或超时
//        // 等待消息或超时
//        if (m_cv.wait_for(lock, std::chrono::milliseconds(timeout_ms), [this]() { return m_message_received; })) {
//            std::cout << "Server response received." << std::endl;
//            return true;
@@ -148,7 +148,7 @@
//    m_message_received = false;
//
//    if (m_connected.load()) {
//        // 发送消息
//        // 发送消息
//        websocketpp::lib::error_code ec;
//        m_client.send(m_hdl, binary_data.data(), binary_data.size(), websocketpp::frame::opcode::binary, ec);
//        if (ec) {
@@ -156,7 +156,7 @@
//            return false;
//        }
//
//        // 等待消息或超时
//        // 等待消息或超时
//        if (m_cv.wait_for(lock, std::chrono::milliseconds(timeout_ms), [this]() { return m_message_received; })) {
//            std::cout << "Server response received." << std::endl;
//            return true;
@@ -275,29 +275,29 @@
//    }
//
//    m_message_received = true;
//    m_cv.notify_one();  // 唤醒等待线程
//    m_cv.notify_one();  // 唤醒等待线程
//}
//
//void WebSocketClient::on_close(connection_hdl hdl) {
//    std::cout << "Connection closed." << std::endl;
//    std::lock_guard<std::mutex> lock(m_mutex);
//
//    m_hdl.reset();  // 清空连接句柄
//    m_hdl.reset();  // 清空连接句柄
//    m_connected.store(false);
//    if (m_close_handler) {
//        m_close_handler();
//    }
//
//    // 如果需要重连
//    // 如果需要重连
//    if (m_reconnect.load()) {
//
//        // 停止当前运行的事件循环
//        // 停止当前运行的事件循环
//        //m_client.stop();
//        //if (m_thread.joinable()) {
//        //    m_thread.join();
//        //}
//        std::cerr << "Connection lost. Attempting to reconnect..." << std::endl;
//        //reconnect();  // 启动重连机制
//        //reconnect();  // 启动重连机制
//    }
//    else {
//        m_cv.notify_one();