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.

73 lines
2.4 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package msg_handler
import (
"git.noahlan.cn/northlan/ntools-go/kafka"
"git.noahlan.cn/northlan/ntools-go/logger"
"live-gateway/config"
"live-gateway/douyu/stt"
pbMq "live-gateway/pb/mq"
pbVars "live-gateway/pb/vars"
kfk "live-gateway/pkg/kafka"
"strconv"
)
// MsgDanmaku 用户在房间发送弹幕时,服务端发此消息给客户端,完整的数据部分应包含的字段如下(挑选有用的)
type (
MsgDanmaku struct {
RoomId int64 `stt:"rid"` // 房间ID
Uid int64 `stt:"uid"` // 用户ID
Nickname string `stt:"nn"` // 昵称
Txt string `stt:"txt"` // 弹幕
Level int32 `stt:"level"` // 用户等级
Rg int32 `stt:"rg"` // 房间权限组默认值1表示普通权限用户 UP:5
Avatar string `stt:"ic"` // 头像地址,后需要添加 _big.jpg 或 _small.jpg 头像获取 https://apic.douyucdn.cn/upload/
FansMedal // 粉丝勋章
Ol int32 `stt:"ol"` // 主播等级
Cid string `stt:"cid"` // 弹幕唯一ID
Cst int64 `stt:"cst"` // 时间戳 ms
Ct int32 `stt:"ct"` // 客户端类型,默认值0
Sahf string `stt:"sahf"` // 扩展字段,一般不用
}
MsgDanmakuHandler struct {
producer *kafka.Producer
liveRoomId int64
}
)
func NewMsgDanmakuHandler(liveRoomId int64) *MsgDanmakuHandler {
cfg := config.Config.Kafka.Danmaku
return &MsgDanmakuHandler{
producer: kafka.NewKafkaProducer(kfk.DefaultProducerConfig, cfg.Addr, cfg.Topic),
liveRoomId: liveRoomId,
}
}
func (m *MsgDanmakuHandler) DataType() string {
return "chatmsg"
}
func (m *MsgDanmakuHandler) HandlerMessage(data []byte) {
var ret MsgDanmaku
err := stt.Unmarshal(data, &ret)
if err != nil {
return
}
logger.SLog.Debugf("%s 说: %s", ret.Nickname, ret.Txt)
dmMsg := &pbMq.MqDanmaku{
Platform: pbVars.Platform_name[int32(pbVars.Platform_Douyu)],
LiveRoomId: m.liveRoomId,
Uid: ret.Uid,
Uname: ret.Nickname,
Avatar: "https://apic.douyucdn.cn/upload/" + ret.Avatar + "_small.jpg", // TODO 暂时组合,应该保留原始,然后客户端配置
Msg: ret.Txt,
MsgId: ret.Cid,
Timestamp: ret.Cst,
FansMedalWearingStatus: ret.FansMedal.MedalRoomId == m.liveRoomId,
FansMedalName: ret.FansMedal.MedalName,
FansMedalLevel: ret.FansMedal.MedalLevel,
}
_ = m.producer.SendMessageAsync(dmMsg, strconv.FormatInt(ret.Uid, 10))
}