50 lines
1.3 KiB
C++
50 lines
1.3 KiB
C++
#pragma once
|
|
|
|
#ifdef NETWORK
|
|
|
|
#include "WebSocketClientBase.h"
|
|
#include <queue>
|
|
#include <boost/beast/core.hpp>
|
|
#include <boost/beast/websocket.hpp>
|
|
#include <boost/asio/connect.hpp>
|
|
#include <boost/asio/ip/tcp.hpp>
|
|
|
|
namespace ZL {
|
|
|
|
class WebSocketClient : public WebSocketClientBase {
|
|
private:
|
|
// Переиспользуем io_context из TaskManager
|
|
boost::asio::io_context& ioc_;
|
|
|
|
// Объекты переехали в члены класса
|
|
std::unique_ptr<boost::beast::websocket::stream<boost::beast::tcp_stream>> ws_;
|
|
boost::beast::flat_buffer buffer_;
|
|
|
|
std::queue<std::string> messageQueue;
|
|
std::mutex queueMutex; // Защита для messageQueue
|
|
|
|
std::queue<std::shared_ptr<std::string>> writeQueue_;
|
|
bool isWriting_ = false;
|
|
std::mutex writeMutex_; // Отдельный мьютекс для очереди записи
|
|
|
|
bool connected = false;
|
|
|
|
|
|
void startAsyncRead();
|
|
void processIncomingMessage(const std::string& msg);
|
|
|
|
public:
|
|
explicit WebSocketClient(boost::asio::io_context& ioc) : ioc_(ioc) {}
|
|
|
|
void Connect(const std::string& host, uint16_t port) override;
|
|
|
|
void Poll() override;
|
|
|
|
void Send(const std::string& message) override;
|
|
void doWrite();
|
|
|
|
bool IsConnected() const override { return connected; }
|
|
};
|
|
}
|
|
#endif
|