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.
杨博文 5ba37f1381
实验报告
2 weeks ago
client 添加p2p功能 2 weeks ago
server 修复语音通话 2 weeks ago
shared 修复语音通话 2 weeks ago
tests 确保所有测试通过 2 weeks ago
.gitignore 图片、文件发送修改 2 weeks ago
README.md GUI正常运行,可以连接到服务器 2 weeks ago
config.py 图片、文件发送修改 2 weeks ago
requirements.txt 确保语音模块测试通过 2 weeks ago
run_client.py 完成 2 weeks ago
run_client_gui.py 添加p2p功能 2 weeks ago
run_server.py 完成 2 weeks ago
实现报告.md 实验报告 2 weeks ago

README.md

P2P Chat - 点对点通信应用

一个基于 Python 的 P2P 网络通信应用程序,支持文本消息、文件传输、图片分享和语音通话。

功能特性

  • 文本消息通信
  • 文件传输(支持断点续传)
  • 图片传输与显示
  • 音视频播放
  • 语音聊天
  • 局域网自动发现
  • 离线消息缓存

环境要求

  • Python 3.9+
  • MySQL 5.7+(可选,用于持久化存储)

安装

1. 克隆项目

git clone <repository-url>
cd chatProgram

2. 创建虚拟环境

python -m venv venv

# Windows
venv\Scripts\activate

# Linux/Mac
source venv/bin/activate

3. 安装依赖

pip install -r requirements.txt

4. 系统依赖Linux

# Ubuntu/Debian
sudo apt install portaudio19-dev ffmpeg libopus-dev

# openEuler/CentOS
sudo dnf install portaudio-devel ffmpeg opus-devel

快速开始

启动服务器

# 基本启动
python run_server.py

# 指定地址和端口
python run_server.py --host 0.0.0.0 --port 8888

# 调试模式
python run_server.py --debug

启动客户端(命令行)

# 基本启动
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

# 基本启动
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 退出

配置

环境变量

# 服务器配置
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. 安装依赖

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. 部署项目

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. 配置防火墙

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

[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

启用服务:

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       # 依赖

测试

# 运行所有测试
pytest

# 运行特定测试
pytest tests/test_relay_server.py -v

# 运行集成测试
pytest tests/test_integration.py -v

License

MIT