废弃的聊天室代码

😭😭😭
This commit is contained in:
zsyg
2025-07-07 20:34:34 +08:00
committed by GitHub
parent 837aba38ba
commit 280a9122b9
29 changed files with 1192 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
#ifndef APPLICATION_HPP
#define APPLICATION_HPP
class MainWindow;
class Application {
public:
Application(int argc, char** argv);
~Application();
int run();
private:
MainWindow* mainWindow;
};
#endif // APPLICATION_HPP

View File

@@ -0,0 +1,36 @@
#ifndef CHATROOM_HPP
#define CHATROOM_HPP
#include "NetworkManager.hpp"
#include "User.hpp"
#include <string>
#include <vector>
#include <memory>
class ChatRoom {
public:
ChatRoom();
~ChatRoom();
bool createRoom(int port, const std::string& password = "");
bool joinRoom(const std::string& ip, int port, const std::string& password);
void sendChatMessage(const std::string& message);
void addUser(const std::string& username);
void removeUser(const std::string& username);
const std::vector<std::string>& getMessages() const;
const std::vector<std::shared_ptr<User>>& getUsers() const;
std::string getCurrentUsername() const;
bool isConnected() const;
NetworkManager* getNetworkManager() const;
std::string getRoomPassword() const;
std::string getLocalIP() const;
private:
std::unique_ptr<NetworkManager> network;
std::vector<std::string> messages;
std::vector<std::shared_ptr<User>> users;
std::string roomPassword;
};
#endif // CHATROOM_HPP

View File

@@ -0,0 +1,31 @@
#ifndef CHATWINDOW_HPP
#define CHATWINDOW_HPP
#include <FL/Fl_Window.H>
#include <FL/Fl_Text_Display.H>
#include <FL/Fl_Text_Buffer.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Browser.H>
#include <memory>
#include "ChatRoom.hpp"
class ChatWindow : public Fl_Window {
public:
ChatWindow(int w, int h, const char* title, std::shared_ptr<ChatRoom> chatRoom);
void appendMessage(const std::string& message);
void updateUserList();
private:
std::shared_ptr<ChatRoom> chatRoom;
Fl_Text_Display* messageDisplay;
Fl_Text_Buffer* messageBuffer;
Fl_Input* messageInput;
Fl_Button* sendButton;
Fl_Browser* userList;
static void onSendMessageCallback(Fl_Widget* w, void* data);
};
#endif // CHATWINDOW_HPP

View File

@@ -0,0 +1,22 @@
#ifndef CONFIGMANAGER_HPP
#define CONFIGMANAGER_HPP
#include <string>
#include <map>
class ConfigManager {
public:
static ConfigManager& getInstance();
void loadConfig(const std::string& filename);
void saveConfig(const std::string& filename);
std::string getValue(const std::string& key);
void setValue(const std::string& key, const std::string& value);
private:
ConfigManager() = default;
std::map<std::string, std::string> configMap;
};
#endif // CONFIGMANAGER_HPP

View File

@@ -0,0 +1,14 @@
#ifndef CONSTANTS_HPP
#define CONSTANTS_HPP
class Constants {
public:
static const int DEFAULT_PORT = 12345;
static const int MAIN_WINDOW_WIDTH = 400;
static const int MAIN_WINDOW_HEIGHT = 300;
static const int CHAT_WINDOW_WIDTH = 800;
static const int CHAT_WINDOW_HEIGHT = 600;
static const char MESSAGE_DELIMITER = '|';
};
#endif // CONSTANTS_HPP

View File

@@ -0,0 +1,45 @@
#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Output.H>
#include <memory>
#include "ChatRoom.hpp"
class MainWindow : public Fl_Window {
public:
MainWindow(int w, int h, const char* title);
~MainWindow();
void showCreateRoomDialog();
void showJoinRoomDialog();
void showUsernameDialog();
private:
Fl_Button* createRoomBtn;
Fl_Button* joinRoomBtn;
Fl_Box* titleBox;
Fl_Window* createRoomDialog;
Fl_Window* joinRoomDialog;
Fl_Window* usernameDialog;
Fl_Input* portInput;
Fl_Input* ipInput;
Fl_Input* passwordInput;
Fl_Input* usernameInput;
std::unique_ptr<ChatRoom> chatRoom;
static void onCreateRoomCallback(Fl_Widget* w, void* data);
static void onJoinRoomCallback(Fl_Widget* w, void* data);
static void onCreateRoomConfirmCallback(Fl_Widget* w, void* data);
static void onJoinRoomConfirmCallback(Fl_Widget* w, void* data);
static void onUsernameConfirmCallback(Fl_Widget* w, void* data);
};
#endif // MAINWINDOW_HPP

View File

@@ -0,0 +1,36 @@
#ifndef MESSAGE_HPP
#define MESSAGE_HPP
#include <string>
#include <chrono>
#include <memory>
#include "User.hpp"
enum class MessageType {
NORMAL, // 普通消息
SYSTEM, // 系统消息
TEXT_MESSAGE, // 文本消息
USER_JOIN, // 用户加入
USER_LEAVE, // 用户离开
AUTH // 认证消息
};
class Message {
public:
Message(const std::shared_ptr<User>& sender,
const std::string& content,
MessageType type = MessageType::NORMAL);
const std::shared_ptr<User>& getSender() const;
const std::string& getContent() const;
MessageType getType() const;
std::chrono::system_clock::time_point getTimestamp() const;
private:
std::shared_ptr<User> sender;
std::string content;
MessageType type;
std::chrono::system_clock::time_point timestamp;
};
#endif // MESSAGE_HPP

View File

@@ -0,0 +1,15 @@
#ifndef MESSAGEPROTOCOL_HPP
#define MESSAGEPROTOCOL_HPP
#include <string>
#include <vector>
#include "Message.hpp"
class MessageProtocol {
public:
static std::string encodeMessage(const Message& message);
static Message decodeMessage(const std::string& data);
static bool validateMessage(const std::string& data);
};
#endif // MESSAGEPROTOCOL_HPP

View File

@@ -0,0 +1,34 @@
#ifndef NETWORKEVENTHANDLER_HPP
#define NETWORKEVENTHANDLER_HPP
#include <memory>
#include "ChatRoom.hpp"
#include "MessageProtocol.hpp"
#include <memory>
#include <thread>
#include <atomic>
#include "ChatRoom.hpp"
#include "Message.hpp"
class NetworkEventHandler {
public:
explicit NetworkEventHandler(std::shared_ptr<ChatRoom> chatRoom);
~NetworkEventHandler();
void start();
void stop();
void onConnected();
void onMessageReceived(const std::string& message);
void onError(const std::string& error);
private:
void heartbeatCheck();
std::shared_ptr<ChatRoom> chatRoom;
std::atomic<bool> running;
std::thread heartbeatThread;
};
#endif // NETWORKEVENTHANDLER_HPP

View File

@@ -0,0 +1,38 @@
#ifndef NETWORKMANAGER_HPP
#define NETWORKMANAGER_HPP
#include <string>
#include <vector>
#include <memory>
#include <thread>
#include <mutex>
#include <functional>
#include <winsock2.h>
class NetworkManager {
public:
using MessageCallback = std::function<void(const std::string&)>;
NetworkManager();
~NetworkManager();
bool startServer(int port);
bool connectToServer(const std::string& ip, int port);
void sendMessage(const std::string& message);
std::vector<std::string> receiveMessages();
bool isConnected() const;
void setMessageCallback(MessageCallback callback) {
messageCallback = callback;
}
private:
SOCKET serverSocket;
SOCKET clientSocket;
std::vector<SOCKET> clients;
std::mutex clientsMutex;
std::thread acceptThread;
MessageCallback messageCallback;
};
#endif // NETWORKMANAGER_HPP

View File

@@ -0,0 +1,29 @@
#ifndef ROOMINFO_HPP
#define ROOMINFO_HPP
#include <string>
class RoomInfo {
public:
RoomInfo(const std::string& name,
const std::string& ip,
int port,
const std::string& password);
const std::string& getName() const;
const std::string& getIpAddress() const;
int getPort() const;
const std::string& getPassword() const;
int getUserCount() const;
void setUserCount(int count);
private:
std::string name;
std::string ipAddress;
int port;
std::string password;
int userCount;
};
#endif // ROOMINFO_HPP

View File

@@ -0,0 +1,15 @@
#ifndef STRINGUTILS_HPP
#define STRINGUTILS_HPP
#include <string>
#include <vector>
class StringUtils {
public:
static std::vector<std::string> split(const std::string& str, char delimiter);
static std::string trim(const std::string& str);
static bool startsWith(const std::string& str, const std::string& prefix);
static bool endsWith(const std::string& str, const std::string& suffix);
};
#endif // STRINGUTILS_HPP

View File

@@ -0,0 +1,22 @@
#ifndef USER_HPP
#define USER_HPP
#include <string>
#include <chrono>
class User {
public:
User(const std::string& username, const std::string& ip);
const std::string& getUsername() const;
const std::string& getIpAddress() const;
bool isActive() const;
void updateLastActive();
private:
std::string username;
std::string ipAddress;
std::chrono::system_clock::time_point lastActive;
};
#endif // USER_HPP

View File

@@ -0,0 +1,20 @@
#ifndef USERMANAGER_HPP
#define USERMANAGER_HPP
#include <algorithm>
#include <vector>
#include <memory>
#include "User.hpp"
class UserManager {
public:
void addUser(std::shared_ptr<User> user);
void removeUser(const std::string& username);
std::vector<std::shared_ptr<User>> getUsers() const;
std::shared_ptr<User> findUser(const std::string& username) const;
private:
std::vector<std::shared_ptr<User>> users;
};
#endif // USERMANAGER_HPP