弹幕直播lib
smt-bullet
解释: 降低直播收发弹幕的开发成本,让业务方更关注于弹幕界面交互效果。
npm
npm使用方法方法,见npm使用说明
npm install @smt-lib/bullet-service
示例
const Studio = require('@smt-lib/bullet-service');
const studio = new Studio({
roomId: '' // 直播间id
});
// 监听消息
studio.onMessage(function (res) {
// res 是消息体
});
// 加入直播间并开始监听消息
studio.join();
.then(res => {
})
.catch(err=> {
});
// 发送消息
studio.sendMessage(text)
.then(res => {
if (res.data && res.data.type === '102') {
studio.quit();
}
})
.catch(err=> {
});
// 退出直播间
studio.quit();
// 扩展api
// 捕获异常
studio.onError(function (err) {
// err 异常信息
});
// 停止接收消息
studio.stop();
// 更新用户openid
studio.updateUser('填写openid');
API
构造器
属性名 | 类型 | 必填 | 默认值 | 说明 |
---|---|---|---|---|
roomId | string | Y | 直播间id | |
openId | string | N | 登录用户必须传openId |
方法
join
加入直播间, 并开始接收消息。
属性名 | 类型 | 必填 | 默认值 | 说明 |
---|---|---|---|---|
roomId | string | N | 构造函数中填写的roomId | 直播间id |
返回:Promise对象
成功
字段名 | 类型 | 必有 | 说明 |
---|---|---|---|
host | object | Y | 主播信息 |
userinfo | object | Y | 用户信息 |
feedbacks | string | Y | 点赞数 |
onlineUsers | string | Y | 在线用户数 |
status | string | Y | 直播间状态,0 直播 2 关闭无回放 3 关闭且有回放 20 直播间结束 |
warning | string | Y | 进入直播间在直播界面显示的警示语,固定为:【官方提醒:欢迎进入直播间。官方提倡健康的直播环境,对弹幕内容进行实时巡查。任何传播违法、违规、低俗等不良信息行为的帐号将会进行严肃处理哦!】。 |
character | string | Y | 用户所属直播间的角色 |
characterName | string | Y | 用户所属直播间的角色名称,如果character为1005普通用户,那么该字段为空,否则不为空; |
hasComment | string | Y | 聊天室是否显示评论区 |
{
"onlineUsers":"0",
"status":"0",
"warning":"官方提醒:欢迎进入直播间。官方提倡健康的直播环境,对弹幕内容进行实时巡查。任何传播违法、违规、低俗等不良信息行为的帐号将会进行严肃处理哦!",
"hasComment":"1",
"feedbacks":"0",
"userinfo":{
"avatar_url":"https://himg.bdimg.com/sys/portrait/item/3ae42f11.jpg?time=3086",
"gender":"2",
"nick_name":"revee789"
},
"host":{
"fans":"65",
"follows":"33",
"gender":"0",
"avatar_url":"https://himg.bdimg.com/sys/portrait/item/tb.1.63ca1926.cEGFpFgU2U04m76uFmWIaw.jpg",
"name":"yanxfchenx",
"nick_name":"yanxfchenx"
}
}
失败
字段名 | 类型 | 必有 | 说明 |
---|---|---|---|
stage | string | Y | 阶段名称: join加入直播间,quit退出直播间,sendMessage发送信息 |
errno | number | N | 错误码 |
errMsg | string | N | 错误信息 |
tipmsg | string | N | 中文错误信息 |
error | string/object | Y | 错误信息 |
sendMessage
字段名 | 类型 | 必有 | 说明 |
---|---|---|---|
text | string | Y | 弹幕文本 |
返回:Promise对象
成功
字段名 | 类型 | 必有 | 说明 |
---|---|---|---|
content | string | Y | 消息内容 |
msgKey | string | Y | 客户端生成id |
msgid | number | Y | 消息id |
失败
字段名 | 类型 | 必有 | 说明 |
---|---|---|---|
stage | string | Y | 阶段名称: join加入直播间,quit退出直播间,sendMessage发送信息 |
errno | number | N | 错误码 |
errMsg | string | N | 错误信息 |
tipmsg | string | N | 中文错误信息 |
error | string/object | Y | 错误信息 |
onMessage
注册消息监听函数
字段名 | 类型 | 必有 | 说明 |
---|---|---|---|
callback | function | Y | 消息回调函数 |
callback参数说明
字段名 | 类型 | 必有 | 说明 |
---|---|---|---|
type | string | Y | 消息类型,可枚举:0文本消息,101系统消息,102退出直播间消息,106删除消息 |
msgid | number | Y | 消息id 唯一 |
// 普通消息
{
"msgKey":"_smt1578212826585",
"msgid":1578212827136553,
"createTime":1578212827,
"roomId":"5619165",
"type":"0",
"vip":"0",
"name":"百度网友cb12e4d",
"portrait":"http://himg.bdimg.com/sys/portrait/item/3ae42f11.jpg?time=3086",
"content":"好纠结的测试文案"
}
// 系统消息
{
"type":101,
"data":{
"lastestuser":[
"张三",
"李四"
],
"onlineusercnt":32,
"totaluser":32,
"onlineuser":[
{
"uid":65,
"avatar":"HTTPS"
}
]
}
// 直播结束
{
"type":102,
"data":{
"duration":718534,
"total_users":0,
"new_fans":0,
"feedbacks":0,
"close_from":0,
"duration_format":"199:35:34",
"total_users_format":"0",
"feedbacks_format":"0",
"msg_num_format":"0"
},
"room_id":"5621662"
}
// 系统公告 二期IM支持
{
"type" :103 // 消息类型
"data":{
"title" : string, //系统消息内容,如 “系统公告”
"content" : string, //系统消息内容
}
}
// 删除消息
{
"type":106,
"data":{
"mcast_id":3307661947,
"msgids":[
"1579175545854083"
]
}
}
tips; 1 type:0 普通消息用于展示。 2 type:101 是系统消息,获取在线人数等信息。 3 type:102 是系统公告,由IM直播服务控制下发。 4 type:106 是删除消息,由IM直播服务控制下发。
quit
退出直播间
退出之前会清除消息监听和内存资源。
字段名 | 类型 | 必有 | 说明 |
---|---|---|---|
roomId | number | N | 房间id |
返回:Promise对象
失败
字段名 | 类型 | 必有 | 说明 |
---|---|---|---|
stage | string | Y | 阶段名称: join加入直播间,quit退出直播间,sendMessage发送信息 |
errno | number | N | 错误码 |
errMsg | string | N | 错误信息 |
tipmsg | string | N | 中文错误信息 |
error | string/object | Y | 错误信息 |
updateUser
在用户切换账号成功后调用,studio实例将自动更新用户数据。
字段名 | 类型 | 必有 | 说明 |
---|---|---|---|
openId | string | Y |
stop
停止接收消息并释放网络资源。
onError
注册异常监听函数
字段名 | 类型 | 必有 | 说明 |
---|---|---|---|
callback | function | Y | 异常回调函数 |
callback参数说明
字段名 | 类型 | 必有 | 说明 |
---|---|---|---|
stage | string | Y | 阶段说明 |
error | string | object | Y |
错误信息说明
errno 错误码与 errmsg对照表
业务方向 业务码 具体业务 具体错误码后缀 错误码信息 错误码解释 直播100
错误码 | 错误信息 | 错误解释 | 备注 |
---|---|---|---|
00001 | unkown error | 下游接口返回错误 | 前缀00 加入直播间 |
00002 | param error | 参数错误 | |
00003 | room not exist | 直播房间号不存在 | |
01001 | room has been shutdown | 直播间被关闭 | 前缀01 发弹幕 |
01002 | no authority | 参数错误 | |
01003 | send msg to restapi-golang cast fail | 向后端发送消息失败 | |
01004 | the user in block list | 用户在黑名单 | |
02001 | param error ext | 参数错误 | 前缀02 退出直播间 |
02002 | token not valid | token无效 | |
02003 | root not ready | 直播未开始 | |
02004 | liveroom size upper limit | 人数已达直播间上限 |
tips:在发生错误时,此表可以做为初步参照依据,进行异常的定位和处理。