Socket Chat - WebSocket + PyQt6 + FastAPI + SQLAlchemy
注册、登录(JWT)
好友添加、列表
单聊消息收发(WebSocket)
消息持久化(PostgreSQL / SQLAlchemy)
桌面客户端(PyQt6)
Python >= 3.12
PostgreSQL (本地或容器)
复制示例并按需修改
cp .env.example .env # Windows: copy .env.example .env
关键项
DATABASE_URL(Postgres 连接串)
JWT_SECRET(JWT 密钥)
API_BASE / WS_URL(客户端指向后端)
# 启动后端(FastAPI + Uvicorn)
python -m server.main
# 启动客户端(PyQt6 桌面端)
python -m client.main
初次运行会自动建库表(SQLAlchemy metadata.create_all)。
推荐用 pgvector 或全文索引按需扩展搜索。
建议将 .env 加入版本忽略,避免提交敏感信息。
安装 uv(Windows PowerShell)
irm https:// astral.sh/ uv/ install.ps1 | iex
创建并使用虚拟环境(Python 3.12+)
uv venv -- python 3.12
uv pip install - e .
运行后端与客户端
uv run python - m server.main
uv run python - m client.main
# 构建并启动(首次会拉取镜像并构建 app)
docker compose up -d --build
# API 将暴露在 http://127.0.0.1:8000
# WebSocket 在 ws://127.0.0.1:8000/ws
# 设置客户端指向容器服务(在本机运行客户端时):
# Windows PowerShell
$env :API_BASE = " http://127.0.0.1:8000" ; $env :WS_URL = " ws://127.0.0.1:8000/ws" ; uv run python -m client.main
# 停止/清理
docker compose down