32 lines
793 B
C++
32 lines
793 B
C++
#pragma once
|
|
#include <boost/beast/websocket.hpp>
|
|
#include <boost/asio.hpp>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <mutex>
|
|
#include <atomic>
|
|
|
|
namespace network {
|
|
|
|
class WebSocketServer {
|
|
public:
|
|
WebSocketServer(uint16_t port);
|
|
~WebSocketServer();
|
|
|
|
void start();
|
|
void broadcast(const std::string& message);
|
|
void stop();
|
|
|
|
private:
|
|
void handleAccept();
|
|
void doRead(std::shared_ptr<boost::beast::websocket::stream<boost::asio::ip::tcp::socket>> ws);
|
|
|
|
boost::asio::io_context ioc_;
|
|
boost::asio::ip::tcp::acceptor acceptor_;
|
|
std::vector<std::weak_ptr<boost::beast::websocket::stream<boost::asio::ip::tcp::socket>>> sessions_;
|
|
std::mutex sessions_mutex_;
|
|
std::atomic<bool> running_{true};
|
|
};
|
|
|
|
} // namespace network
|