You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
4.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# P2P Chat - 点对点通信应用
一个基于 Python 的 P2P 网络通信应用程序,支持文本消息、文件传输、图片分享和语音通话。
## 功能特性
- 文本消息通信
- 文件传输(支持断点续传)
- 图片传输与显示
- 音视频播放
- 语音聊天
- 局域网自动发现
- 离线消息缓存
## 环境要求
- Python 3.9+
- MySQL 5.7+(可选,用于持久化存储)
## 安装
### 1. 克隆项目
```bash
git clone <repository-url>
cd chatProgram
```
### 2. 创建虚拟环境
```bash
python -m venv venv
# Windows
venv\Scripts\activate
# Linux/Mac
source venv/bin/activate
```
### 3. 安装依赖
```bash
pip install -r requirements.txt
```
### 4. 系统依赖Linux
```bash
# Ubuntu/Debian
sudo apt install portaudio19-dev ffmpeg libopus-dev
# openEuler/CentOS
sudo dnf install portaudio-devel ffmpeg opus-devel
```
## 快速开始
### 启动服务器
```bash
# 基本启动
python run_server.py
# 指定地址和端口
python run_server.py --host 0.0.0.0 --port 8888
# 调试模式
python run_server.py --debug
```
### 启动客户端(命令行)
```bash
# 基本启动
python run_client.py --username alice
# 连接远程服务器
python run_client.py -u bob -s 192.168.1.100 -p 8888
# 带显示名称
python run_client.py -u alice -d "Alice Wang"
```
### 启动客户端GUI
```bash
# 基本启动
python run_client_gui.py
# 指定服务器
python run_client_gui.py --server 192.168.1.100 --port 8888
```
## 命令行客户端命令
连接成功后可使用以下命令:
| 命令 | 说明 |
|------|------|
| `/list` | 查看在线用户 |
| `/msg <用户> <消息>` | 发送消息 |
| `/file <用户> <路径>` | 发送文件 |
| `/call <用户>` | 发起语音通话 |
| `/endcall` | 结束通话 |
| `/stats` | 查看统计信息 |
| `/refresh` | 刷新用户列表 |
| `/help` | 查看帮助 |
| `/quit` | 退出 |
## 配置
### 环境变量
```bash
# 服务器配置
P2P_SERVER_HOST=0.0.0.0
P2P_SERVER_PORT=8888
P2P_MAX_CONNECTIONS=1000
# 数据库配置
P2P_DB_HOST=localhost
P2P_DB_PORT=3306
P2P_DB_NAME=p2p_chat
P2P_DB_USER=root
P2P_DB_PASSWORD=your_password
# 安全配置
P2P_USE_TLS=true
P2P_CERT_FILE=/path/to/cert.pem
P2P_KEY_FILE=/path/to/key.pem
```
### 配置文件
也可以直接修改 `config.py` 中的默认值。
## 部署到 openEuler 服务器
### 1. 安装依赖
```bash
sudo dnf update -y
sudo dnf install -y python3 python3-pip python3-devel
sudo dnf install -y portaudio-devel ffmpeg opus-devel mysql-server mysql-devel
```
### 2. 部署项目
```bash
mkdir -p /opt/p2p-chat
cd /opt/p2p-chat
# 上传项目文件
# scp -r ./* user@server:/opt/p2p-chat/
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
```
### 3. 配置防火墙
```bash
sudo firewall-cmd --permanent --add-port=8888/tcp
sudo firewall-cmd --permanent --add-port=8889/udp
sudo firewall-cmd --reload
```
### 4. 设置系统服务
创建 `/etc/systemd/system/p2p-chat.service`
```ini
[Unit]
Description=P2P Chat Server
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/opt/p2p-chat
Environment=PATH=/opt/p2p-chat/venv/bin
ExecStart=/opt/p2p-chat/venv/bin/python run_server.py
Restart=always
[Install]
WantedBy=multi-user.target
```
启用服务:
```bash
sudo systemctl daemon-reload
sudo systemctl enable p2p-chat
sudo systemctl start p2p-chat
sudo systemctl status p2p-chat
```
## 项目结构
```
chatProgram/
├── client/ # 客户端模块
│ ├── ui/ # GUI 组件
│ ├── app.py # 客户端应用
│ ├── connection_manager.py
│ ├── file_transfer.py
│ ├── image_processor.py
│ ├── media_player.py
│ └── voice_chat.py
├── server/ # 服务器模块
│ ├── database.py
│ ├── relay_server.py
│ └── repositories.py
├── shared/ # 共享模块
│ ├── message_handler.py
│ ├── models.py
│ └── security.py
├── tests/ # 测试
├── config.py # 配置
├── run_server.py # 服务器启动脚本
├── run_client.py # 命令行客户端启动脚本
├── run_client_gui.py # GUI 客户端启动脚本
└── requirements.txt # 依赖
```
## 测试
```bash
# 运行所有测试
pytest
# 运行特定测试
pytest tests/test_relay_server.py -v
# 运行集成测试
pytest tests/test_integration.py -v
```
## License
MIT