|
@@ -33,8 +33,9 @@ void WebsocketServer::start() {
|
|
// 开始接受连接
|
|
// 开始接受连接
|
|
asio::post(boss_context_, [this] { doAccept(); });
|
|
asio::post(boss_context_, [this] { doAccept(); });
|
|
|
|
|
|
- spdlog::info("[Server] Started on {} with {} threads.",
|
|
|
|
|
|
+ spdlog::info("WebsocketServer Started on {}:{} with {} threads.",
|
|
acceptor_.local_endpoint().address().to_string(),
|
|
acceptor_.local_endpoint().address().to_string(),
|
|
|
|
+ acceptor_.local_endpoint().port(),
|
|
worker_pool_.size() + 1);
|
|
worker_pool_.size() + 1);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -59,7 +60,7 @@ void WebsocketServer::stop() {
|
|
for (auto& t : boss_threads_) {
|
|
for (auto& t : boss_threads_) {
|
|
if (t.joinable()) t.join();
|
|
if (t.joinable()) t.join();
|
|
}
|
|
}
|
|
- spdlog::info("[Server] Stopped.");
|
|
|
|
|
|
+ spdlog::info("WebsocketServer Stopped.");
|
|
}
|
|
}
|
|
|
|
|
|
void WebsocketServer::broadcast(const Message& msg) const
|
|
void WebsocketServer::broadcast(const Message& msg) const
|
|
@@ -84,12 +85,12 @@ void WebsocketServer::doAccept() {
|
|
acceptor_.async_accept(
|
|
acceptor_.async_accept(
|
|
[this](boost::system::error_code ec, tcp::socket socket) {
|
|
[this](boost::system::error_code ec, tcp::socket socket) {
|
|
if (!ec) {
|
|
if (!ec) {
|
|
- spdlog::info("[Server] New connection accepted");
|
|
|
|
|
|
+ spdlog::info("WebsocketServer New connection accepted");
|
|
// 设置 Socket 选项
|
|
// 设置 Socket 选项
|
|
setupSocketOptions(socket);
|
|
setupSocketOptions(socket);
|
|
// 将新连接的 socket 移交给 worker_context_
|
|
// 将新连接的 socket 移交给 worker_context_
|
|
worker_pool_.post([this, s = std::move(socket)]() mutable {
|
|
worker_pool_.post([this, s = std::move(socket)]() mutable {
|
|
- spdlog::info("[Worker] New session lambda executed");
|
|
|
|
|
|
+ spdlog::info("Worker New session lambda executed");
|
|
|
|
|
|
auto session = std::make_shared<WebsocketSession>(std::move(s));
|
|
auto session = std::make_shared<WebsocketSession>(std::move(s));
|
|
|
|
|
|
@@ -112,7 +113,7 @@ void WebsocketServer::doAccept() {
|
|
// 继续接受新连接
|
|
// 继续接受新连接
|
|
doAccept();
|
|
doAccept();
|
|
} else if (ec != asio::error::operation_aborted) {
|
|
} else if (ec != asio::error::operation_aborted) {
|
|
- spdlog::error("[Server] Accept error: {}", ec.message());
|
|
|
|
|
|
+ spdlog::error("WebsocketServer Accept error: {}", ec.message());
|
|
|
|
|
|
// 防止高频错误导致CPU占用过高,使用 `steady_timer` 延迟重新监听
|
|
// 防止高频错误导致CPU占用过高,使用 `steady_timer` 延迟重新监听
|
|
auto timer = std::make_shared<asio::steady_timer>(boss_context_, std::chrono::milliseconds(100));
|
|
auto timer = std::make_shared<asio::steady_timer>(boss_context_, std::chrono::milliseconds(100));
|