# blivemsg
[](https://crates.io/crates/blivemsg)
[](https://docs.rs/blivemsg)
[](https://github.com/urlynn/blivemsg/blob/main/LICENSE)
**Bilibili Live Message Library** - 简单但完整的B站直播弹幕 Rust 库
## 🚀 快速开始
### 基础示例
#### Stream 模式(推荐)
```rust,no_run
use blivemsg::BliveClient;
use futures_util::StreamExt;
#[tokio::main]
async fn main() -> Result<(), blivemsg::Error> {
// 创建客户端(从 Cookie 文件加载)
let mut client = BliveClient::new(7734200, "cookies.json")?;
// 获取消息流
let mut stream = client.stream().await?;
// 处理消息
while let Some(message) = stream.next().await {
match message {
Message::Danmu(d) => println!("[{}] {}: {}", d.medal_level, d.username, d.content),
Message::Gift(g) => println!("{} 送了 {} x{}", g.username, g.gift_name, g.num),
Message::SuperChat(sc) => println!("[SC] ¥{:.2}", sc.price),
_ => {}
}
}
Ok(())
}
```
## ✨ 核心特性
- **52+ 消息类型** - 完整支持弹幕、礼物、SC、舰长等所有消息
- **异步流式 API** - 基于 `Stream` trait,符合 Rust 生态惯例
- **零拷贝解析** - 高效的 JSON 解析,最小化内存分配
- **自动重连** - WebSocket 断开时自动恢复连接
- **类型安全** - 强类型消息定义,编译期错误检查
## 📖 文档
- **[API 文档](https://docs.rs/blivemsg)** - 完整的 API 参考
- **[GitHub](https://github.com/urlynn/blivemsg)** - 源码仓库、Issue 反馈
- **[消息列表](https://github.com/urlynn/blivemsg/blob/main/MESSAGES.md)** - 所有支持的消息类型
## 🔧 高级用法
### 使用回调模式
```rust,no_run
use blivemsg::BliveClient;
#[tokio::main]
async fn main() -> Result<(), blivemsg::Error> {
let mut client = BliveClient::new(7734200, "cookies.json")?;
// 只处理弹幕
client.on_danmu(|danmu| {
println!("[{}] {}: {}", danmu.medal_level, danmu.username, danmu.content);
}).await?;
Ok(())
}
```
### 便捷方法
```rust,no_run
use blivemsg::BliveClient;
#[tokio::main]
async fn main() -> Result<(), blivemsg::Error> {
let mut client = BliveClient::new(7734200, "cookies.json")?;
// 只处理礼物
client.on_gift(|gift| {
println!("{} 送了 {} x{}", gift.username, gift.gift_name, gift.num);
}).await?;
Ok(())
}
```
## 📦 Cookie 配置
创建 `cookies.json` 文件:
```json
{
"SESSDATA": "your_SESSDATA",
"buvid3": "your_buvid3"
}
```
## 🛠️ Features
- **default**: 最小依赖,仅库功能
- **cli**: 启用 CLI 工具(需要 clap、toml)
- **protobuf-support**: 启用 Protobuf 消息支持
- **emulation**: 浏览器指纹模拟, 仅做备用选项(需要 wreq-util)
## 📄 许可证
MIT
## 🔗 相关链接
- [GitHub](https://github.com/urlynn/blivemsg)
- [crates.io](https://crates.io/crates/blivemsg)
- [docs.rs](https://docs.rs/blivemsg)