From 0511259075e638cf8e8f30d0f01553feb0503996 Mon Sep 17 00:00:00 2001 From: amass Date: Fri, 10 Jan 2025 19:07:43 +0800 Subject: [PATCH] remove unused chatroom. --- Server/Application.h | 2 - Server/CMakeLists.txt | 1 - Server/ChatRoom/CMakeLists.txt | 10 --- Server/ChatRoom/ChatRoom.cpp | 31 --------- Server/ChatRoom/ChatRoom.h | 23 ------- Server/ChatRoom/WebSocketChatSession.cpp | 88 ------------------------ Server/ChatRoom/WebSocketChatSession.h | 53 -------------- Server/conf/server.conf | 11 --- 8 files changed, 219 deletions(-) delete mode 100644 Server/ChatRoom/CMakeLists.txt delete mode 100644 Server/ChatRoom/ChatRoom.cpp delete mode 100644 Server/ChatRoom/ChatRoom.h delete mode 100644 Server/ChatRoom/WebSocketChatSession.cpp delete mode 100644 Server/ChatRoom/WebSocketChatSession.h diff --git a/Server/Application.h b/Server/Application.h index 98fa2c8..2f104a2 100644 --- a/Server/Application.h +++ b/Server/Application.h @@ -9,7 +9,6 @@ #include class HttpSession; -class ChatRoom; class SystemUsage; class IoContext; @@ -53,7 +52,6 @@ private: std::shared_ptr m_ioContext; std::shared_ptr> m_router; std::shared_ptr m_timer; - std::shared_ptr m_charRoom; std::shared_ptr m_systemUsage; std::shared_ptr m_replyer; }; diff --git a/Server/CMakeLists.txt b/Server/CMakeLists.txt index dba82f8..a88e363 100644 --- a/Server/CMakeLists.txt +++ b/Server/CMakeLists.txt @@ -1,6 +1,5 @@ find_package(Boost COMPONENTS program_options json process REQUIRED) -add_subdirectory(ChatRoom) add_subdirectory(WebRTC) add_executable(Server main.cpp diff --git a/Server/ChatRoom/CMakeLists.txt b/Server/ChatRoom/CMakeLists.txt deleted file mode 100644 index d3d95aa..0000000 --- a/Server/ChatRoom/CMakeLists.txt +++ /dev/null @@ -1,10 +0,0 @@ - - -add_library(ChatRoom - ChatRoom.h ChatRoom.cpp - WebSocketChatSession.h WebSocketChatSession.cpp -) - -target_link_libraries(ChatRoom - PUBLIC Universal -) \ No newline at end of file diff --git a/Server/ChatRoom/ChatRoom.cpp b/Server/ChatRoom/ChatRoom.cpp deleted file mode 100644 index f85e430..0000000 --- a/Server/ChatRoom/ChatRoom.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "ChatRoom.h" -#include "WebSocketChatSession.h" - -void ChatRoom::join(WebSocketSession *session) { - std::lock_guard lock(m_mutex); - m_sessions.insert(session); -} - -void ChatRoom::leave(WebSocketSession *session) { - std::lock_guard lock(m_mutex); - m_sessions.erase(session); -} - -void ChatRoom::send(std::string message) { - auto const ss = std::make_shared(std::move(message)); - - // Make a local list of all the weak pointers representing - // the sessions, so we can do the actual sending without - // holding the mutex: - std::vector> v; - { - std::lock_guard lock(m_mutex); - v.reserve(m_sessions.size()); - for (auto p : m_sessions) v.emplace_back(p->weak_from_this()); - } - - // For each session in our local list, try to acquire a strong - // pointer. If successful, then send the message on that session. - for (auto const &wp : v) - if (auto sp = wp.lock()) sp->send(ss); -} \ No newline at end of file diff --git a/Server/ChatRoom/ChatRoom.h b/Server/ChatRoom/ChatRoom.h deleted file mode 100644 index 186c6db..0000000 --- a/Server/ChatRoom/ChatRoom.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef __CHATROOM_H__ -#define __CHATROOM_H__ - -#include "Singleton.h" -#include -#include - -class WebSocketSession; - -class ChatRoom { -public: - void join(WebSocketSession *session); - void leave(WebSocketSession *session); - /** - * @brief Broadcast a message to all websocket client sessions - */ - void send(std::string message); - -private: - std::mutex m_mutex; - std::unordered_set m_sessions; -}; -#endif // __CHATROOM_H__ \ No newline at end of file diff --git a/Server/ChatRoom/WebSocketChatSession.cpp b/Server/ChatRoom/WebSocketChatSession.cpp deleted file mode 100644 index 8080403..0000000 --- a/Server/ChatRoom/WebSocketChatSession.cpp +++ /dev/null @@ -1,88 +0,0 @@ -#include "WebSocketChatSession.h" -#include "ChatRoom.h" -#include "StringUtility.h" -#include -#include -#include - -WebSocketSession::WebSocketSession(boost::asio::ip::tcp::socket &&socket) : m_ws(std::move(socket)) { -} - -WebSocketSession::~WebSocketSession() { - auto chatRoom = Amass::Singleton::instance(); - chatRoom->leave(this); -} - -void WebSocketSession::onAccept(boost::beast::error_code ec) { - if (ec) { - if (ec == boost::asio::error::operation_aborted || ec == boost::beast::websocket::error::closed) return; - std::cerr << "accept: " << ec.message() << "\n"; - return; - } - LOG(info) << "accept websocket target: " << m_target; - if (m_target.find("/webrtc") == 0) { - auto splits = Amass::StringUtility::split(m_target, "/"); - if (!splits.empty()) m_id = splits.back(); - } - - auto chatRoom = Amass::Singleton::instance(); - chatRoom->join(this); - - // Read a message - m_ws.async_read(m_buffer, boost::beast::bind_front_handler(&WebSocketSession::on_read, shared_from_this())); -} - -void WebSocketSession::on_read(boost::beast::error_code ec, std::size_t) { - if (ec) { - // Don't report these - if (ec == boost::asio::error::operation_aborted || ec == boost::beast::websocket::error::closed) return; - LOG(error) << "read: " << ec.message(); - return; - } - auto message = boost::beast::buffers_to_string(m_buffer.data()); - LOG(info) << message; - auto chatRoom = Amass::Singleton::instance(); - chatRoom->send(message); // Send to all connections - - m_buffer.consume(m_buffer.size()); // Clear the buffer - m_ws.async_read(m_buffer, boost::beast::bind_front_handler(&WebSocketSession::on_read, shared_from_this())); -} - -void WebSocketSession::send(std::shared_ptr const &ss) { - // Post our work to the strand, this ensures - // that the members of `this` will not be - // accessed concurrently. - m_ws.text(); - boost::asio::post(m_ws.get_executor(), - boost::beast::bind_front_handler(&WebSocketSession::onSend, shared_from_this(), ss)); -} - -void WebSocketSession::onSend(std::shared_ptr const &ss) { - // Always add to queue - m_queue.push_back(ss); - - // Are we already writing? - if (m_queue.size() > 1) return; - - // We are not currently writing, so send this immediately - m_ws.async_write(boost::asio::buffer(*m_queue.front()), - boost::beast::bind_front_handler(&WebSocketSession::on_write, shared_from_this())); -} - -void WebSocketSession::on_write(boost::beast::error_code ec, std::size_t) { - // Handle the error, if any - if (ec) { - // Don't report these - if (ec == boost::asio::error::operation_aborted || ec == boost::beast::websocket::error::closed) return; - std::cerr << "write: " << ec.message() << "\n"; - return; - } - - // Remove the string from the queue - m_queue.erase(m_queue.begin()); - - // Send the next message if any - if (!m_queue.empty()) - m_ws.async_write(boost::asio::buffer(*m_queue.front()), - boost::beast::bind_front_handler(&WebSocketSession::on_write, shared_from_this())); -} diff --git a/Server/ChatRoom/WebSocketChatSession.h b/Server/ChatRoom/WebSocketChatSession.h deleted file mode 100644 index 05f3a27..0000000 --- a/Server/ChatRoom/WebSocketChatSession.h +++ /dev/null @@ -1,53 +0,0 @@ -#ifndef WEBSOCKETSESSION_H -#define WEBSOCKETSESSION_H - -#include "BoostLog.h" -#include -#include -#include -#include -#include - -/** - * @brief Represents an active WebSocket connection to the server - */ -class WebSocketSession : public std::enable_shared_from_this { -public: - WebSocketSession(boost::asio::ip::tcp::socket &&socket); - - ~WebSocketSession(); - - template - void run(boost::beast::http::request> request) { - using namespace boost::beast::http; - using namespace boost::beast::websocket; - // Set suggested timeout settings for the websocket - m_ws.set_option(stream_base::timeout::suggested(boost::beast::role_type::server)); - m_target = request.target(); - // Set a decorator to change the Server of the handshake - m_ws.set_option(stream_base::decorator([](response_type &response) { - response.set(field::server, std::string(BOOST_BEAST_VERSION_STRING) + " websocket-chat-multi"); - })); - // LOG(info) << request.base().target(); //get path - // Accept the websocket handshake - m_ws.async_accept(request, [self{shared_from_this()}](boost::beast::error_code ec) { self->onAccept(ec); }); - } - - // Send a message - void send(std::shared_ptr const &ss); - -protected: - void onAccept(boost::beast::error_code ec); - void on_read(boost::beast::error_code ec, std::size_t bytes_transferred); - void on_write(boost::beast::error_code ec, std::size_t bytes_transferred); - void onSend(std::shared_ptr const &ss); - -private: - std::string m_target; - boost::beast::flat_buffer m_buffer; - boost::beast::websocket::stream m_ws; - std::vector> m_queue; - std::string m_id; -}; - -#endif // WEBSOCKETSESSION_H diff --git a/Server/conf/server.conf b/Server/conf/server.conf index f83bdf0..5c4e0f9 100644 --- a/Server/conf/server.conf +++ b/Server/conf/server.conf @@ -91,17 +91,6 @@ location ~ /notify.*$ { proxy_pass http://local; } -location /Younger/ChatRoom { - - proxy_pass http://local; - - proxy_http_version 1.1; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection "Upgrade"; - proxy_set_header Host $host; - proxy_read_timeout 1200s; -} - #error_page 404 /404.html; # redirect server error pages to the static page /50x.html