直播间
直播间插件(Live)为 Monibuca V6 提供 实时互动直播 能力。适用于秀场直播、社交直播、电商直播等场景。
- 1 主播 + N 观众:支持 100+ 并发观众,无人数上限
- 实时互动系统:
- 礼物系统 & 连击(Combo)支持
- 连麦系统(Link-Mic),主播可邀请观众连麦
- PK 对战,主播间实时竞赛
- 机器人观众:模拟观众用于测试和演示
- 弹幕聊天:弹幕发送、速率限制、内容审查
- 数据持久化:直播间信息、礼物记录、观众数据存储到数据库
- 管控能力:踢人、封禁、聊天静音等主播权限
features = ["live"]
# Live 插件依赖 Room 服务# room feature 会自动启用graph TB subgraph Live["LivePlugin"] SM["SessionManager<br/>直播间信息、礼物系统"] GiftSys["GiftSystem<br/>Combo 连击检测"] LinkMic["LinkMicSystem<br/>连麦请求队列"] RobotMgr["RobotManager<br/>模拟观众"] DB["数据持久化<br/>直播间、礼物、观众"] end
subgraph RS["Room Service"] Room["房间管理<br/>主播 + 观众"] WS["WebSocket<br/>信令通信"] API["RoomApi"] end
subgraph Media["媒体层"] WHIP["WebRTC WHIP<br/>推流"] WHEP["WebRTC WHEP<br/>拉流"] end
Live -->|register_callbacks| RS SM --> DB GiftSys --> SM LinkMic --> SM RobotMgr --> Room Room --> API API -->|broadcast_to_room<br/>add_ban, remove_user| Live WS --> Live WHIP --> Media WHEP --> Media直播间生命周期
Section titled “直播间生命周期”Preparing → Living → Paused → Ended| 状态 | 说明 |
|---|---|
Preparing | 直播准备中,尚未开始 |
Living | 直播进行中,观众可加入 |
Paused | 直播暂停,观众保留,稍后可恢复 |
Ended | 直播已结束,房间关闭 |
| Action | 方向 | 说明 |
|---|---|---|
start_live | C→S | 主播开始直播 |
stop_live | C→S | 主播结束直播 |
pause_live | C→S | 主播暂停直播 |
resume_live | C→S | 主播恢复直播 |
| Action | 方向 | 说明 |
|---|---|---|
send_gift | C→S | 观众发送礼物 |
gift_received | S→C | 礼物通知(广播) |
| Action | 方向 | 说明 |
|---|---|---|
link_mic_request | C→S | 观众申请连麦 |
link_mic_accept | C→S | 主播接受连麦 |
link_mic_reject | C→S | 主播拒绝连麦 |
link_mic_end | C→S | 连麦结束 |
link_mic_timeout | S→C | 连麦邀请超时 |
PK 对战信令
Section titled “PK 对战信令”| Action | 方向 | 说明 |
|---|---|---|
pk_invite | C→S | 主播 A 邀请主播 B 进行 PK |
pk_accept | C→S | 主播 B 接受 PK |
pk_reject | C→S | 主播 B 拒绝 PK |
pk_end | C→S | 结束 PK |
pk_score_update | S→C | PK 分数实时更新(广播) |
| Action | 方向 | 说明 |
|---|---|---|
send_danmaku | C→S | 发送弹幕 |
danmaku | S→C | 弹幕通知(广播) |
{ "action": "send_gift", "room_id": "live_100001", "gift_id": "rocket", "count": 1, "combo_id": "combo_abc123"}连击(Combo)机制
Section titled “连击(Combo)机制”礼物支持连击,客户端在短时间内多次发送同一礼物时使用相同的 combo_id:
// 礼物 1{ "action": "send_gift", "gift_id": "rocket", "count": 1, "combo_id": "combo_xyz789"}
// 礼物 2(同一连击){ "action": "send_gift", "gift_id": "rocket", "count": 1, "combo_id": "combo_xyz789"}
// 服务端响应{ "action": "gift_received", "gift_id": "rocket", "combo_count": 2, "combo_id": "combo_xyz789"}- 超时时间内未继续发送同一礼物 → 连击结束
- 客户端可显示”连击 × N”的动画效果
- 支持配置最大连击次数限制
观众申请 → 主播接受/拒绝 → 建立 WebRTC 连接 → 连麦中 → 结束{ "action": "link_mic_request", "room_id": "live_100001"}主播接受连麦
Section titled “主播接受连麦”{ "action": "link_mic_accept", "room_id": "live_100001", "target_user_id": "user_789"}live: link_mic: max_participants: 6 # 最大连麦人数(包括主播) timeout: 30 # 连麦邀请超时(秒) auto_end_on_decline: true # 拒绝后自动清理主播 A 邀请主播 B → 主播 B 接受/拒绝 → 建立 PK 房间 → 实时计分 → 结束{ "action": "pk_invite", "target_anchor_id": "anchor_123"}PK 期间,系统根据以下因素实时计算双方分数:
- 礼物贡献:观众送礼
- 弹幕互动:弹幕数量与活跃度
- 时间系数:PK 时长加权
live: pk: enable: true # 启用 PK 功能 timeout: 300 # PK 邀请超时(秒) duration: 600 # 单场 PK 时长(秒) scoring: gift_multiplier: 1.5 # 礼物贡献系数 danmaku_weight: 0.1 # 弹幕权重- 测试直播功能:模拟真实观众行为
- 演示:增加直播间热度
- 压力测试:评估系统承载能力
{ "action": "add_robots", "room_id": "live_100001", "count": 50, "behaviors": { "gift_probability": 0.3, // 30% 概率发送礼物 "danmaku_probability": 0.5, // 50% 概率发送弹幕 "link_mic_probability": 0.05 // 5% 概率申请连麦 }}{ "action": "send_danmaku", "content": "哈哈哈"}- 速率限制:防止刷屏
- 内容审查:敏感词过滤
- 用户等级:VIP 用户可能有不同限制
- 超时清理:自动删除过期弹幕
速率限制配置
Section titled “速率限制配置”live: danmaku: max_per_minute: 10 # 每分钟最多 10 条 max_length: 200 # 最大字长 cooldown: 2 # 两条弹幕最小间隔(秒)HTTP API
Section titled “HTTP API”curl -X POST http://localhost:8080/live/rooms \ -H "Content-Type: application/json" \ -d '{ "anchor_id": "anchor_001", "title": "今晚秀场", "cover_url": "https://example.com/cover.jpg" }'获取直播间列表
Section titled “获取直播间列表”curl http://localhost:8080/live/rooms获取直播间详情
Section titled “获取直播间详情”curl http://localhost:8080/live/rooms/{room_id}获取礼物统计
Section titled “获取礼物统计”curl http://localhost:8080/live/rooms/{room_id}/gift-stats数据库持久化
Section titled “数据库持久化”直播间表 live_rooms
Section titled “直播间表 live_rooms”| 字段 | 类型 | 说明 |
|---|---|---|
id | string | 房间 ID |
anchor_id | string | 主播 ID |
title | string | 直播标题 |
status | enum | 直播状态 |
start_time | timestamp | 开始时间 |
end_time | timestamp | 结束时间 |
viewer_count | int | 观看人数 |
total_gift_amount | decimal | 礼物总额 |
礼物记录表 live_gifts
Section titled “礼物记录表 live_gifts”| 字段 | 类型 | 说明 |
|---|---|---|
id | string | 礼物 ID |
room_id | string | 房间 ID |
sender_id | string | 发送者 ID |
gift_type | string | 礼物类型 |
count | int | 数量 |
amount | decimal | 金额 |
created_at | timestamp | 创建时间 |
live: # 礼物系统 gift: combo_timeout: 5 # 连击超时(秒) max_combo_count: 999 # 最大连击数
# 连麦系统 link_mic: max_participants: 6 # 最大连麦人数 timeout: 30 # 邀请超时(秒)
# PK 对战 pk: enable: true timeout: 300 # 邀请超时(秒) duration: 600 # 对战时长(秒)
# 弹幕系统 danmaku: max_per_minute: 10 max_length: 200 cooldown: 2
# 数据库 database: enable: true # 启用持久化 table_prefix: "live_" # 表名前缀
# 机器人观众 robot: enable: true # 启用机器人 max_robots: 10000 # 最大机器人数Web-SDK 集成
Section titled “Web-SDK 集成”const room = document.getElementById('room') as MbRoom;const publisher = document.getElementById('publisher') as MbPublisher;
// 连接直播间room.setAttribute('ws-url', 'ws://localhost:8080/room?type=live');room.setAttribute('room-id', 'live_100001');
// 主播推流await publisher.startCapture({ audio: true, video: true });await publisher.startPublish();const room = document.getElementById('room') as MbRoom;
// 发送礼物room.sendMessage({ action: 'send_gift', gift_id: 'rocket', count: 1, combo_id: 'combo_123'});// 申请连麦room.sendMessage({ action: 'link_mic_request'});
// 监听连麦响应room.addEventListener('link_mic_accept', (event) => { // 建立连麦 WebRTC 连接 await publisher.startPublish();});// 发送弹幕room.sendMessage({ action: 'send_danmaku', content: '主播加油!'});
// 监听弹幕room.addEventListener('danmaku', (event) => { console.log(`${event.username}: ${event.content}`);});Demo 项目
Section titled “Demo 项目”完整的直播间 Demo 位于 web-sdk/packages/demo/live/:
live/├── src/│ ├── pages/│ │ ├── Lobby/index.tsx # 直播列表│ │ ├── LiveRoom/index.tsx # 直播间(主播/观众)│ │ └── GiftPanel/index.tsx # 礼物面板│ ├── components/│ │ ├── Danmaku/index.tsx # 弹幕组件│ │ ├── Viewers/index.tsx # 观众列表│ │ └── GiftEffect/index.tsx # 礼物特效│ ├── services/api.ts # API 封装│ └── types/index.ts # 类型定义├── package.json # port: 5474└── vite.config.ts启动 Demo
Section titled “启动 Demo”cd web-sdk/packages/demo/livepnpm installpnpm dev# 访问 http://localhost:5474http://localhost:5474?role=anchor&anchor_id=anchor_001http://localhost:5474?role=viewer&room_id=live_100001Demo 自动检测设备类型,移动端采用:
- 竖屏全屏直播视频
- 底部浮动弹幕栏
- 右侧浮动礼物/互动按钮(安全区域)
- 上拉唤出礼物面板
- 长按发送弹幕
联系我们
微信公众号:不卡科技
腾讯频道:流媒体技术
QQ 频道:p0qq0crz08
QQ 群:751639168