douyulive

package module
v0.0.0-...-59f69b0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2021 License: MIT Imports: 21 Imported by: 0

README

douyu-barrage

斗鱼弹幕服务器接入--golang实现

功能
  1. 接入斗鱼弹幕服务器,获取直播间弹幕、礼物、动态消息等
安装
go get -u github.com/BaoJW/douyu-barrage
aid和secret获取
需要去斗鱼申请接入的开发者权限
注意:若申请权限就没有办法获取token,从而无法接入斗鱼弹幕服务器
https://open.douyu.com/manage/ 
斗鱼文档
https://open.douyu.com/source/
快速开始
func main() {
	aid := flag.String("aid", "", "aid")
	secret := flag.String("secret", "", "secret")
	roomID := flag.Int("roomId", 0, "roomId")
	ip := flag.String("ip", "", "ip")
	port := flag.Int("port", 0, "port")

	flag.Parse()

	if *aid == "" {
		log.Fatalln("aid不能为空!")
		return
	}

	if *secret == "" {
		log.Fatalln("secret不能为空!")
		return
	}

	if *roomID <= 0 {
		log.Fatalln("房间号错误!")
		return
	}

	//远程获取pprof数据
	go func() {
		log.Println(http.ListenAndServe("localhost:8080", nil))
	}()

	live := &douyulive.Live{
		Debug:              false, // 不输出日志
		AnalysisRoutineNum: 1,     // 消息分析协程数量,默认为1,为1可以保证通知顺序与接收到消息顺序相同
		LoginRespMessageHandler: func(roomID int, msg *douyulive.LoginRespMessageModel) {
			log.Printf("【登录消息】%s 登录成功", msg.NickName)
		},
		BarrageMessageHandler: func(roomID int, msg *douyulive.BarrageMessageModel) {
			log.Printf("【弹幕消息】%s 说:%s", msg.NickName, msg.Txt)
		},
		StormMessageHandler: func(roomID int, msg *douyulive.StormMessage) {
			log.Printf("【领取在线鱼丸消息】%s 在 %d 直播间领取%d鱼丸", msg.NickName, msg.RoomID, msg.Sil)
		},
		SendGiftMessageHandler: func(roomID int, msg *douyulive.SendGiftMessage) {
			log.Printf("【赠送礼物消息】%s在%d直播间赠送%d礼物,礼物ID: %d", msg.NickName, msg.RoomID, msg.GfCount, msg.GiftID)
		},
		SpecialUserMessageHandler: func(roomID int, msg *douyulive.SpecialUserMessage) {
			log.Printf("【用户进房通知消息】欢迎 %s 进入直播间%d", msg.NickName, msg.RoomID)
		},
		SwitchBroadcastMessageHandler: func(roomID int, msg *douyulive.SwitchBroadcastMessage) {
			if msg.Status == 0 {
				log.Printf("【直播间开关播提醒消息】 %d直播间当前没有直播", msg.RoomID)
			} else if msg.Status == 1 {
				log.Printf("【直播间开关播提醒消息】 %d直播间当前正在直播", msg.RoomID)
			}

		},
		BroadcastRankMessageHandler: func(roomID int, msg *douyulive.BroadcastRankMessage) {
			log.Printf("【排行榜消息】 %d直播间当前时间%s排行榜消息 总榜: %v, 周榜: %v, 日榜: %v", msg.RoomID, time.Unix(msg.Timestamp, 0), msg.ListAll, msg.List, msg.ListDay)
		},
		SuperBarrageMessageHandler: func(roomID int, msg *douyulive.SuperBarrageMessage) {
			log.Printf("【超级弹幕消息】 %d直播间超级弹幕消息: %s", msg.RoomID, msg.Content)
		},
		RoomGiftBarrageMessageHandler: func(roomID int, msg *douyulive.RoomGiftBroadcastMessage) {
			log.Printf("【房间内礼物广播消息】 %d直播间 %s赠送给 %s %d个 %d", msg.RoomID, msg.SendNickName, msg.DoneeNickName, msg.GiftCount, msg.GiftID)
		},
	}
	live.Start(context.Background())
	_ = live.Join(*aid, *secret, *ip, *port, *roomID)
	live.Wait()
}

最后
各位老爷们如果觉得好用,就给小的一个star吧

Documentation

Index

Constants

View Source
const (
	HeadLen    uint32 = 4
	MsgTypeLen uint32 = 2
	KeepLen    uint32 = 2
)
View Source
const (
	LoginRespType             = "loginresp"
	BarrageRespType           = "chatmsg"
	StormRespType             = "onlinegift"
	SendGiftRespType          = "dgb"
	SpecialUserRespType       = "uenter"
	SwitchBroadcastRespType   = "rss"
	BroadcastRankRespType     = "ranklist"
	SuperBarrageRespType      = "ssd"
	RoomGiftBroadcastRespType = "spbc"
)

Variables

This section is empty.

Functions

func ByteToMsg

func ByteToMsg(data []byte) map[string]string

解析数据

func GenerateToken

func GenerateToken(aid, secret string, currentTime time.Time) (string, error)

func MarshalDouYuData

func MarshalDouYuData(resp []byte, module interface{}) (interface{}, error)

func Md5

func Md5(key string) string

md5哈希加密

func MsgToByte

func MsgToByte(msg map[string]string) []byte

func StrToInt64

func StrToInt64(s string) int64

Types

type BarrageInfo

type BarrageInfo struct {
	List        []*BarrageList `json:"list"`
	Count       int64          `json:"cnt"`
	PageContext int64          `json:"page_context"`
}

弹幕信息

type BarrageList

type BarrageList struct {
	RoomID    int64  `json:"room_id"`   // 房间号
	UID       int64  `json:"uid"`       // 用户uid
	Nickname  string `json:"nickname"`  // 主播昵称
	Content   string `json:"content"`   // 弹幕内容
	Timestamp int64  `json:"timestamp"` // 发送时间戳
	Ip        string `json:"ip"`        // 用户IP
	Platform  string `json:"platform"`  // 直播平台
}

type BarrageMessageModel

type BarrageMessageModel struct {
	Type                string    `json:"type"`   // 表示为“弹幕”消息,固定为 chatmsg
	GroupID             int64     `json:"gid"`    // 弹幕组id
	RoomID              int64     `json:"rid"`    // 房间id
	UID                 int64     `json:"uid"`    // 发送者uid
	NickName            string    `json:"nn"`     // 发送者昵称
	Txt                 string    `json:"txt"`    // 弹幕文本内容
	CID                 int64     `json:"cid"`    // 弹幕唯一ID
	Level               int64     `json:"level"`  // 用户等级
	GiftTitle           int64     `json:"gt"`     // 礼物头衔:默认值 0(表示没有头衔)
	Color               int64     `json:"col"`    // 颜色:默认值 0(表示默认颜色弹幕)
	ClientType          int64     `json:"ct"`     // 客户端类型:默认值 0
	RoomGroup           int64     `json:"rg"`     // 房间权限组:默认值 1(表示普通权限用户)
	PlatformGroup       int64     `json:"pg"`     // 平台权限组:默认值 1(表示普通权限用户)
	DiligentLevel       int64     `json:"dlv"`    // 酬勤等级:默认值 0(表示没有酬勤)
	DiligentCount       int64     `json:"dc"`     // 酬勤数量:默认值 0(表示没有酬勤数量)
	BestDiligentLevel   int64     `json:"bdlv"`   // 最高酬勤等级:默认值 0(表示全站都没有酬勤)
	ChatMsgType         int64     `json:"cmt"`    // 弹幕具体类型: 默认值 0(普通弹幕)
	Sahf                int64     `json:"sahf"`   // 扩展字段,一般不使用,可忽略
	Ic                  string    `json:"ic"`     // 用户头像
	NobleLevel          int64     `json:"nl"`     // 贵族等级
	NobleChat           int64     `json:"nc"`     // 贵族弹幕标识,0-非贵族弹幕,1-贵族弹幕,默认值 0
	GatewayTimestampIn  int64     `json:"gatin"`  // 进入网关服务时间戳
	GatewayTimestampOut int64     `json:"gatout"` // 离开网关服务时间戳
	ChtIn               int64     `json:"chtin"`  // 进入房间服务时间戳
	ChtOut              int64     `json:"chtout"` // 离开房间服务时间戳
	Repin               int64     `json:"repin"`  // 进入发送服务时间戳
	Repout              int64     `json:"repout"` // 离开发送服务时间戳
	BadgeNickName       string    `json:"bnn"`    // 徽章昵称
	BadgeLevel          int64     `json:"bl"`     // 徽章等级
	BadgeRoomID         int64     `json:"brid"`   // 徽章房间 id
	Hc                  int64     `json:"hc"`     // 徽章信息校验码
	AnchorLevel         int64     `json:"ol"`     // 主播等级
	Reserve             int64     `json:"rev"`    // 是否反向弹幕标记: 0-普通弹幕,1-反向弹幕, 默认值 0
	HighLight           int64     `json:"hl"`     // 否高亮弹幕标记: 0-普通,1-高亮, 默认值 0
	Ifs                 int64     `json:"ifs"`    // 是否粉丝弹幕标记: 0-非粉丝弹幕,1-粉丝弹幕, 默认值 0
	P2P                 int64     `json:"p2p"`    // 服务功能字段
	El                  *ElDetail `json:"el"`     // 用户获得的连击特效
}

BarrageMessageModel 弹幕消息模型

func TransferBarrageMessage

func TransferBarrageMessage(data map[string]string) *BarrageMessageModel

type BroadcastRankMessage

type BroadcastRankMessage struct {
	Type      string      `json:"type"`     // 表示为“广播排行榜消息”,固定为 ranklist
	RoomID    int64       `json:"rid"`      // 房间ID
	Timestamp int64       `json:"ts"`       // 排行榜更新时间戳
	Sequex    int64       `json:"seq"`      // 排行榜序列号
	GroupID   int64       `json:"gid"`      // 弹幕分组ID
	ListAll   *ListDetail `json:"list_all"` // 总榜
	List      *ListDetail `json:"list"`     // 周榜
	ListDay   *ListDetail `json:"list_day"` // 日榜
}

广播排行榜消息

func TransferBroadcastRankMessage

func TransferBroadcastRankMessage(data map[string]string) *BroadcastRankMessage

type DouYuResponse

type DouYuResponse struct {
	Code int64       `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

斗鱼接口数据返回

type ElDetail

type ElDetail struct {
	EID   int64 `json:"eid"` // 特效 id
	EType int64 `json:"etp"` // 特效类型
	Sc    int64 `json:"sc"`  // 特效次数
	Ef    int64 `json:"ef"`  // 特效标志
}

type ListDetail

type ListDetail struct {
	UID         int64 `json:"uid"`       // 用户 id
	LastRank    int64 `json:"lrk"`       // 上次排名
	CurrentRank int64 `json:"crk"`       // 当前排名
	Rs          int64 `json:"rs"`        // 排名变化,-1:下降,0:持平,1:上升
	GoldCost    int64 `json:"gold_cost"` // 当前贡献值
}

榜单明细

type Live

type Live struct {
	Debug                         bool                                 // 是否显示日志
	AnalysisRoutineNum            int                                  // 消息分析协程数量,默认为1,为1可以保证通知顺序与接收到消息顺序相同
	LoginRespMessageHandler       func(int, *LoginRespMessageModel)    // 登录响应消息handler
	BarrageMessageHandler         func(int, *BarrageMessageModel)      // 弹幕消息handler
	StormMessageHandler           func(int, *StormMessage)             // 领取在线鱼丸暴击消息handler
	SendGiftMessageHandler        func(int, *SendGiftMessage)          // 赠送礼物消息handler
	SpecialUserMessageHandler     func(int, *SpecialUserMessage)       // 用户进房通知消息handler
	SwitchBroadcastMessageHandler func(int, *SwitchBroadcastMessage)   // 房间开关播消息handler
	BroadcastRankMessageHandler   func(int, *BroadcastRankMessage)     // 广播排行榜消息handler
	SuperBarrageMessageHandler    func(int, *SuperBarrageMessage)      // 超级弹幕消息handler
	RoomGiftBarrageMessageHandler func(int, *RoomGiftBroadcastMessage) // 房间内礼物广播消息handler
	// contains filtered or unexported fields
}

Live 直播间

func (*Live) Join

func (live *Live) Join(aid, secret, ip string, port int, roomIDs ...int) error

Join 添加房间

func (*Live) Remove

func (live *Live) Remove(roomIDs ...int) error

Remove 移出房间

func (*Live) Start

func (live *Live) Start(ctx context.Context)

Start 开始接收

func (*Live) Wait

func (live *Live) Wait()

type LoginRespMessageModel

type LoginRespMessageModel struct {
	Type          string `json:"type"`       // 表示为“登录”消息,固定为 loginres
	UserID        int64  `json:"userid"`     // 用户 ID
	RoomGroup     int64  `json:"roomgroup"`  // 房间权限组
	PlatformGroup int64  `json:"pg"`         // 平台权限组
	SessioniID    int64  `json:"sessionid"`  // 会话ID
	UserName      string `json:"username"`   // 用户名
	NickName      string `json:"nickname"`   // 用户昵称
	LiveStat      int64  `json:"live_stat"`  // 直播状态
	IsIllegal     int64  `json:"is_illegal"` // 是否违规
	IllContent    string `json:"ill_ct"`     // 违规提醒内容
	IllTimestamp  int64  `json:"ill_ts"`     // 违规提醒开始时间戳
	Now           int64  `json:"now"`        // 系统当前时间
	Ps            int64  `json:"ps"`         // 手机绑定标示
	Es            int64  `json:"es"`         // 邮箱绑定标示
	It            int64  `json:"it"`         // 认证类型
	Its           int64  `json:"its"`        // 认证状态
	Npv           int64  `json:"npv"`        // 是否需要手机验证
	BestDlev      int64  `json:"best_dlev"`  // 最高酬勤等级
	CurLev        int64  `json:"cur_lev"`    // 酬勤等级
	Nrc           int64  `json:"nrc"`        // 观看房间需要的条件
	Ih            int64  `json:"ih"`         // 是否进房隐身
	SID           int64  `json:"sid"`        // 服务 id
	Sahf          int64  `json:"sahf"`       // 扩展字段,一般不使用,可忽略
}

登录响应消息模型

func TransferLoginRespMessage

func TransferLoginRespMessage(data map[string]string) *LoginRespMessageModel

type RoomGiftBroadcastMessage

type RoomGiftBroadcastMessage struct {
	Type          string `json:"type"` // 表示为“房间内礼物广播”,固定为 spbc
	RoomID        int64  `json:"rid"`  // 房间ID
	GroupID       int64  `json:"gid"`  // 弹幕分组ID
	SendNickName  string `json:"sn"`   // 赠送者昵称
	DoneeNickName string `json:"dn"`   // 受赠者昵称
	GiftName      int64  `json:"gn"`   // 礼物名称
	GiftCount     int64  `json:"gc"`   // 礼物数量
	DoneeRoomID   int64  `json:"drid"` // 赠送房间
	Gs            int64  `json:"gs"`   // 广播样式
	Gb            int64  `json:"gb"`   // 是否有礼包(0-无礼包,1-有礼包)
	Es            int64  `json:"es"`   // 广播展现样式(1-火箭,2-飞机)
	GiftID        int64  `json:"gfid"` // 礼物ID
	EID           int64  `json:"eid"`  // 特效 id
	Bgl           int64  `json:"bgl"`  // 广播礼物类型
	Ifs           int64  `json:"ifs"`  // 服务功能字段,可忽略
	Cl2           int64  `json:"cl2"`  // 栏目分类广播字段
}

房间内礼物广播

func TransferRoomGiftBroadcastMessage

func TransferRoomGiftBroadcastMessage(data map[string]string) *RoomGiftBroadcastMessage

type RoomInfo

type RoomInfo struct {
	RoomID   int64  `json:"rid"`       // 房间号
	Attendee int64  `json:"hn"`        // 人气值
	RoomName string `json:"room_name"` // 房间名
}

直播间信息

type SendGiftMessage

type SendGiftMessage struct {
	Type     string `json:"type"`  // 表示为“赠送礼物”消息,固定为 dgb
	RoomID   int64  `json:"rid"`   // 房间ID
	GroupID  int64  `json:"gid"`   // 弹幕分组ID
	GiftID   int64  `json:"gfid"`  // 礼物 id
	Gs       int64  `json:"gs"`    // 礼物显示样式
	UserID   int64  `json:"uid"`   // 用户ID
	NickName string `json:"nn"`    // 用户昵称
	Bg       int64  `json:"bg"`    // 大礼物标识:默认值为 0(表示是小礼物)
	Ic       int64  `json:"ic"`    // 用户头像
	EID      int64  `json:"eid"`   // 礼物关联的特效 id
	Level    int64  `json:"level"` // 用户等级
	Dw       int64  `json:"dw"`    // 主播体重
	GfCount  int64  `json:"gfcnt"` // 礼物个数:默认值 1(表示 1 个礼物)
	Hits     int64  `json:"hits"`  // 礼物连击次数:默认值 1(表示 1 连击)
	Dlv      int64  `json:"dlv"`   // 酬勤头衔:默认值 0(表示没有酬勤)
	Dc       int64  `json:"dc"`    // 酬勤个数:默认值 0(表示没有酬勤数量)
	Bdl      int64  `json:"bdl"`   // 全站最高酬勤等级:默认值 0(表示全站都没有酬勤)
	Rg       int64  `json:"rg"`    // 房间身份组:默认值 1(表示普通权限用户)
	Pg       int64  `json:"pg"`    // 平台身份组:默认值 1(表示普通权限用户)
	RpID     int64  `json:"rpid"`  // 扩展字段 id
	RpIDn    int64  `json:"rpidn"` // 扩展字段 id
	Slt      int64  `json:"slt"`   // 扩展字段,一般不使用
	Elt      int64  `json:"elt"`   // 扩展字段,一般不使用
	Nl       int64  `json:"nl"`    // 贵族等级:默认值 0(表示不是贵族)
	Sahf     int64  `json:"sahf"`  // 扩展字段,一般不使用,可忽略
	BNN      string `json:"bnn"`   // 徽章昵称
	BL       int64  `json:"bl"`    // 徽章等级
	Brid     int64  `json:"brid"`  // 徽章房间 id
	Hc       int64  `json:"hc"`    // 徽章信息校验码
	Fc       int64  `json:"fc"`    // 攻击道具的攻击力
}

赠送礼物消息 用户在房间赠送礼物时,服务端发送此消息给客户端

func TransferSendGiftMessage

func TransferSendGiftMessage(data map[string]string) *SendGiftMessage

type SpecialUserMessage

type SpecialUserMessage struct {
	Type     string    `json:"type"`  // 表示为“用户进房通知”消息,固定为 uenter
	RoomID   int64     `json:"rid"`   // 房间ID
	GroupID  int64     `json:"gid"`   // 弹幕分组ID
	NickName string    `json:"nn"`    // 用户昵称
	Str      int64     `json:"str"`   // 战斗力
	Level    int64     `json:"level"` // 新用户等级
	Gt       int64     `json:"gt"`    // 礼物头衔:默认值 0(表示没有头衔)
	Rg       int64     `json:"rg"`    // 房间权限组:默认值 1(表示普通权限用户)
	Pg       int64     `json:"pg"`    // 平台身份组:默认值 1(表示普通权限用户)
	Dlv      int64     `json:"dlv"`   // 酬勤等级:默认值 0(表示没有酬勤)
	Dc       int64     `json:"dc"`    // 酬勤数量:默认值 0(表示没有酬勤数量)
	Bdlv     int64     `json:"bdlv"`  // 最高酬勤等级:默认值 0
	Ic       int64     `json:"ic"`    // 用户头像
	Nl       int64     `json:"nl"`    // 贵族等级
	CeID     int64     `json:"ceid"`  // 扩展功能字段 id
	Crw      int64     `json:"crw"`   // 用户栏目上周排名
	Ol       int64     `json:"ol"`    // 主播等级
	El       *ElDetail `json:"el"`
	Sahf     int64     `json:"sahf"` // 扩展字段,一般不使用,可忽略
	Wgei     int64     `json:"wgei"` // 页游欢迎特效 id

}

用户进房通知消息 具有特殊属性的用户进入直播间时,服务端发送此消息至客户端

func TransferSpecialUserMessage

func TransferSpecialUserMessage(data map[string]string) *SpecialUserMessage

type StormMessage

type StormMessage struct {
	Type          string `json:"type"`  // 表示为“领取在线鱼丸”消息,固定为 onlinegift
	RoomID        int64  `json:"rid"`   // 房间ID
	UserID        int64  `json:"uid"`   // 用户ID
	GroupID       int64  `json:"gid"`   // 弹幕分组ID
	Sil           int64  `json:"sil"`   // 鱼丸数
	If            int64  `json:"if"`    // 领取鱼丸的等级
	Ct            int64  `json:"ct"`    // 客户端类型标识
	NickName      string `json:"nn"`    // 用户昵称
	Ur            int64  `json:"ur"`    // 鱼丸之刃倍率
	Level         int64  `json:"level"` // 用户等级
	BroadcastType int64  `json:"btype"` // 广播类型
}

领取在线鱼丸暴击消息 在线领取鱼丸时,若出现暴击,服务则发送领取暴击消息到客户端。

func TransferStormMessage

func TransferStormMessage(data map[string]string) *StormMessage

type SuperBarrageMessage

type SuperBarrageMessage struct {
	Type       string `json:"type"`    // 表示为“超级弹幕”消息,固定为 ssd
	RoomID     int64  `json:"rid"`     // 房间ID
	GroupID    int64  `json:"gid"`     // 弹幕分组ID
	SDID       int64  `json:"sdid"`    // 超级弹幕 id
	TRID       int64  `json:"trid"`    // 跳转房间 id
	Content    string `json:"content"` // 超级弹幕的内容
	Url        string `json:"url"`     // 跳转url
	ClientType int64  `json:"clitp"`   // 客户端类型
	JumpType   int64  `json:"jmptp"`   // 跳转类型
}

超级弹幕消息

func TransferSuperBarrageMessage

func TransferSuperBarrageMessage(data map[string]string) *SuperBarrageMessage

type SwitchBroadcastMessage

type SwitchBroadcastMessage struct {
	Type    string `json:"type"`    // 表示为“房间开播提醒”消息,固定为 rss
	RoomID  int64  `json:"rid"`     // 房间ID
	GroupID int64  `json:"gid"`     // 弹幕分组ID
	Status  int64  `json:"ss"`      // 直播状态,0-没有直播,1-正在直播
	Code    int64  `json:"code"`    // 类型
	Rt      int64  `json:"rt"`      // 开关播原因
	Rtv     int64  `json:"rtv"`     // 关播原因类型的值
	Notify  int64  `json:"notify"`  // 通知类型
	Endtime int64  `json:"endtime"` // 关播时间(仅关播时有效)
}

直播间开关播提醒

func TransferSwitchBroadcastMessage

func TransferSwitchBroadcastMessage(data map[string]string) *SwitchBroadcastMessage

type TokenInfo

type TokenInfo struct {
	Token  string `json:"token"`
	Expire int    `json:"expire"` // 过期时间,2小时
}

Token返回信息

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL