You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.6 KiB
Go

3 years ago
package msg_handler
import (
"encoding/json"
"git.noahlan.cn/northlan/ntools-go/kafka"
"git.noahlan.cn/northlan/ntools-go/logger"
"live-gateway/config"
"live-gateway/pb"
kfk "live-gateway/pkg/kafka"
"strconv"
3 years ago
)
//var _ bilibili.MsgHandler = (*DanmakuHandler)(nil)
// Danmaku 弹幕结构,未解析完全,但足够使用
type Danmaku struct {
PlayerMode int `json:"player_mode"` // 用户模式
FontSize int `json:"font_size"` // 字体大小
DanmakuColor int64 `json:"danmaku_color"` // 弹幕颜色
SendTime int64 `json:"send_time"` // 弹幕发送时间
ID int64 `json:"id"` // 弹幕ID
MsgType int `json:"msg_type"` // 消息类型
Bubble string `json:"bubble"` // 弹幕气泡
Content string `json:"content"` // 弹幕内容
UID int64 `json:"uid"` // 用户ID
Uname string `json:"uname"` // 用户名
RoomAdmin bool `json:"room_admin"` // 是否房管
VipMonth bool `json:"vip_month"` // 是否月费姥爷
VipYear bool `json:"vip_year"` // 是否年费姥爷
Rank int `json:"rank"` // 排名
MobileVerify bool `json:"mobile_verify"` // 是否验证手机号
UnameColor string `json:"uname_color"` // 用户名颜色
MedalLevel int `json:"medal_level"` // 粉丝勋章等级
MedalName string `json:"medal_name"` // 勋章名
MedalUpName string `json:"medal_up_name"` // up主名称
UserLevel int `json:"user_level"` // 用户等级
}
type DanmakuHandler struct {
producer *kafka.Producer
liveRoomId int64
}
func NewDanmakuHandler(liveRoomId int64) *DanmakuHandler {
cfg := config.Config.Kafka.Danmaku
return &DanmakuHandler{
producer: kafka.NewKafkaProducer(kfk.DefaultProducerConfig, cfg.Addr, cfg.Topic),
liveRoomId: liveRoomId,
}
3 years ago
}
func (d *DanmakuHandler) CMD() string {
return "DANMU_MSG"
}
func (d *DanmakuHandler) HandlerMessage(data []byte) {
var baseMsg struct {
CMD string `json:"cmd"`
Info []interface{} `json:"info"`
}
if err := json.Unmarshal(data, &baseMsg); err != nil {
return
}
dm := &Danmaku{}
info := baseMsg.Info
l := len(info)
if l >= 1 {
h := info[0].([]interface{})
dm.PlayerMode = int(h[1].(float64))
dm.FontSize = int(h[2].(float64))
dm.DanmakuColor = int64(h[3].(float64))
dm.SendTime = int64(h[4].(float64))
dm.ID = int64(h[5].(float64))
dm.MsgType = int(h[10].(float64))
dm.Bubble = h[11].(string)
}
if l >= 2 {
dm.Content = info[1].(string)
}
if l >= 3 {
h := info[2].([]interface{})
dm.UID = int64(h[0].(float64))
dm.Uname = h[1].(string)
dm.RoomAdmin = floatToBool(h[2].(float64))
dm.VipMonth = floatToBool(h[3].(float64))
dm.VipYear = floatToBool(h[4].(float64))
dm.Rank = int(h[5].(float64))
dm.MobileVerify = floatToBool(h[6].(float64))
dm.UnameColor = h[7].(string)
}
if l >= 4 {
h := info[3].([]interface{})
l2 := len(h)
if l2 >= 1 {
dm.MedalLevel = int(h[0].(float64))
}
if l2 >= 2 {
dm.MedalName = h[1].(string)
}
if l2 >= 3 {
dm.MedalUpName = h[2].(string)
}
}
if l >= 5 {
h := info[4].([]interface{})
dm.UserLevel = int(h[0].(float64))
}
logger.SLog.Debugf("%s 说: %s", dm.Uname, dm.Content)
dmMsg := &pbMq.MqDanmaku{
Platform: pbMq.Platform_name[int32(pbMq.Platform_bilibili)],
LiveRoomId: d.liveRoomId,
Uid: dm.UID,
Uname: dm.Uname,
Msg: dm.Content,
Timestamp: dm.SendTime,
}
_ = d.producer.SendMessageAsync(dmMsg, strconv.FormatInt(dm.UID, 10))
3 years ago
}
func floatToBool(v float64) bool {
if v == 0 {
return false
}
if v == 1 {
return true
}
return false
}