diff --git a/CMakeLists.txt b/CMakeLists.txt index 791c21c..fdbd31f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ message(STATUS "YC_DEBUG option: ${YC_DEBUG}") src/lib/base.cpp src/core/config.cpp src/core/server.cpp + src/core/ssl_server.cpp src/core/chat_room.cpp src/lib/tools.cpp src/core/chat_user.cpp diff --git a/src/core/ssl_server.cpp b/src/core/ssl_server.cpp index c14b4ab..212881f 100644 --- a/src/core/ssl_server.cpp +++ b/src/core/ssl_server.cpp @@ -212,10 +212,13 @@ void SSLServer::handleWebSocketMessage(struct lws *wsi, const std::string& messa bool added = false; for (auto &roomObj: _rooms) { if (roomObj->name() == room) { - // Create ChatUser and add to room - auto chatUser = std::make_shared(name, color, token, lws_get_socket_fd(wsi)); + // Add user to room (ChatUser will be created by addUser) if (roomObj->addUser(name, color, password, lws_get_socket_fd(wsi))) { - _users[token] = chatUser; + // Find the created ChatUser + auto chatUser = roomObj->findUserByName(name); + if (chatUser) { + _users[token] = chatUser; + } added = true; break; } @@ -244,7 +247,7 @@ void SSLServer::handleWebSocketMessage(struct lws *wsi, const std::string& messa // Process message through room for (auto &room: _rooms) { if (room->userIsInRoom(user->name())) { - room->addMessage(user, msg); + room->addMessage(ChatUser::MsgType::message, msg, user->name(), user->color()); break; } } @@ -310,13 +313,13 @@ void SSLServer::broadcastToRoom(const std::string& roomName, const std::string& void SSLServer::createRooms() { // Load rooms from database or config - // This is a simplified version - would need to be expanded Json::Value roomList = _config->group("rooms"); if (roomList.isArray()) { for (const auto& room : roomList) { - // Create room objects - // This would need the actual ChatRoom constructor + // Create room objects using the same logic as the main server + auto newRoom = std::make_shared(nullptr, room); // parent will be set later + _rooms.push_back(newRoom); } } } diff --git a/src/core/ssl_server.h b/src/core/ssl_server.h index ba966be..a750aca 100644 --- a/src/core/ssl_server.h +++ b/src/core/ssl_server.h @@ -50,6 +50,7 @@ public: // Message handling void sendMessage(int socket, const std::string& message); + void sendMessage(int socket, const Json::Value& message); void broadcastToRoom(const std::string& roomName, const std::string& message); // WebSocket callbacks diff --git a/src/lib/base.cpp b/src/lib/base.cpp index 90b1733..befc3b4 100755 --- a/src/lib/base.cpp +++ b/src/lib/base.cpp @@ -242,5 +242,20 @@ namespace Yc { return inputTree; } + std::string Base::generateToken() { + static const char charset[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + static const size_t charset_size = sizeof(charset) - 1; + + std::string token; + token.reserve(32); + + // Einfacher Zufallsgenerator für Token + for (int i = 0; i < 32; ++i) { + token += charset[rand() % charset_size]; + } + + return token; + } + } // namespace Lib } // namespace Yc diff --git a/src/lib/base.h b/src/lib/base.h index b509e1a..7b87070 100755 --- a/src/lib/base.h +++ b/src/lib/base.h @@ -27,6 +27,7 @@ protected: static void unmarkWebSocket(int socket); static bool isWebSocket(int socket); static std::string webSocketAcceptKey(const std::string& clientKey); + static std::string generateToken(); }; } // namespace Lib diff --git a/src/main.cpp b/src/main.cpp index 136946b..fde0be5 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,7 @@ #include "core/config.h" #include "core/server.h" +#include "core/ssl_server.h" #include "lib/database.h" #include @@ -9,19 +10,20 @@ int main(int, char **) { auto config = std::make_shared(); auto database = std::make_shared(config); - // Check if SSL is enabled (for future implementation) + // Check if SSL is enabled bool sslEnabled = config->value("server", "ssl_enabled").asBool(); if (sslEnabled) { - std::cout << "[YourChat] SSL/TLS support is configured but not yet implemented" << std::endl; - std::cout << "[YourChat] Starting without SSL/TLS support" << std::endl; + std::cout << "[YourChat] Starting with SSL/TLS support" << std::endl; + auto sslServer = std::make_shared(config, database); + sslServer->createRooms(); + sslServer->run(); } else { std::cout << "[YourChat] Starting without SSL/TLS support" << std::endl; + auto server = std::make_shared(config, database); + server->createRooms(config->group("rooms")); + server->run(); } - auto server = std::make_shared(config, database); - server->createRooms(config->group("rooms")); - server->run(); - return 0; }