a test module of a simple chatroom with simple constructure and basic features
Project description
多人聊天室中枢通讯模块设计
- chatrooms
---来自某人的综合设计---
English version Русская версия
设计思想
- 目的:快速构建消息通讯
- 作用:搭建模拟“群聊”的通讯架构
- 实际:将群聊逻辑做成极简通信模块,使得只需要构造类就可以完成通信
- 注:后续可能会继续跟进以适应更多情况
模块使用(快速上手)
安装
# 从 PyPI 安装
pip install fastchatrooms
# 或从源码安装
pip install -e .
# 安装测试依赖
pip install fastchatrooms[test]
基本使用
1. 初始化聊天室
from fastchatrooms import ChatRoom
# 创建聊天室
chatroom = ChatRoom('Test Room')
2. 添加用户
# 添加用户
chatroom.add('user1', 'User 1')
chatroom.add('user2', 'User 2')
# 获取所有用户
users = chatroom.participants()
print(f"Current users: {[user.name for user in users]}")
3. 发送消息
# 广播消息(发送给所有人)
chatroom.send('user1', 'Hello everyone!')
# 私聊消息(发送给特定用户)
chatroom.send_to('user1', 'Hello private!', 'user2')
4. 获取历史消息
# 获取最近的消息
messages = chatroom.history(limit=10)
for msg in messages:
print(f"{msg.sender_name}: {msg.content}")
# 获取特定用户的消息
user_messages = chatroom.history_by_sender('user1', limit=5)
# 获取消息总数
message_count = chatroom.history_count()
print(f"Total messages: {message_count}")
5. 删除消息
# 删除单条消息
chatroom.delete_message('message_id')
# 删除特定用户的所有消息
deleted_count = chatroom.delete_user_messages('user1')
print(f"Deleted {deleted_count} messages from user1")
# 清空所有消息
chatroom.clear_history()
6. 事件回调
# 消息回调
def on_message(msg):
print(f"New message: {msg.sender_name}: {msg.content}")
chatroom.on_message(on_message)
# 加入回调
def on_join(user):
print(f"User joined: {user.name}")
chatroom.on_join(on_join)
# 离开回调
def on_leave(user):
print(f"User left: {user.name}")
chatroom.on_leave(on_leave)
7. 清空所有数据
# 清空所有数据库文件
chatroom.clear_all()
8. 停止聊天室
# 停止聊天室,关闭数据库连接
chatroom.stop()
技术栈设计
- 实现语言:Python (version:3.13)
- 初步设计数据库:SQLite
- 测试框架:pytest
- 许可证:MIT License
模块结构和组分
- 核心部分架构
fastchatrooms/
├── store/
│ ├── __init__.py # 导出
│ ├── base.py # 存储抽象基类
│ └── sqlitestore.py # SQLite 实现
├── __init__.py # 导出主要类
├── message.py # Message 类
├── user.py # User 类
└── chatroom.py # ChatRoom 核心类
- 总架构 fastchatrooms/ ├── fastchatrooms/ │ ├── init.py │ ├── chatroom.py │ ├── message.py │ ├── user.py │ └── store/ │ ├── init.py │ ├── base.py │ └── sqlitestore.py ├── test/ │ ├── init.py │ ├── test_chatroom.py │ └── test_clear_all.py ├── setup.py ├── setup.cfg ├── pyproject.toml ├── README.md ├── LICENSE └── requirements.txt
其他信息
- 仓库:
https://github.com/ComplicatedWinds/fastchatrooms.git - 设计:
B.H.C. - 初步包装日期:
2026年3月18日
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
fastchatrooms-0.1.0.tar.gz
(13.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastchatrooms-0.1.0.tar.gz.
File metadata
- Download URL: fastchatrooms-0.1.0.tar.gz
- Upload date:
- Size: 13.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3585a7a4806aada1c0eadb137d10bb997464b8265bb046f3d67615c5997bd71e
|
|
| MD5 |
0e52e77fc3673f53ea4770c998cb8040
|
|
| BLAKE2b-256 |
976b4fcbb23ebb1b43ece14e7c29cd798e287d0e36fa2e5d7b3574b020031959
|
File details
Details for the file fastchatrooms-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastchatrooms-0.1.0-py3-none-any.whl
- Upload date:
- Size: 14.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
523a919cfff9ab51cb8ce82a84e141b12af9f547e57dd69faa9e76c6edc29f90
|
|
| MD5 |
6b0698214351612b446d6d0f80101897
|
|
| BLAKE2b-256 |
b873dc27cd63cd479ebb70e92997c04984ba4bc52422d29aef1059671f200144
|