Merge remote-tracking branch 'upstream/main'

This commit is contained in:
aynakeya
2025-04-24 23:00:19 +08:00
14 changed files with 224 additions and 185 deletions

2
.gitignore vendored
View File

@@ -1,3 +1,3 @@
.idea .idea
.vscode .vscode
scripts scripts

View File

@@ -44,6 +44,7 @@ func NewClient(roomID int) *Client {
customEventHandlers: &customEventHandlers{}, customEventHandlers: &customEventHandlers{},
done: ctx.Done(), done: ctx.Done(),
cancel: cancel, cancel: cancel,
lock: sync.RWMutex{},
api: nil, api: nil,
lock: sync.RWMutex{}, lock: sync.RWMutex{},
} }

View File

@@ -21,7 +21,8 @@ type eventHandlers struct {
superChatHandlers []func(*message.SuperChat) superChatHandlers []func(*message.SuperChat)
giftHandlers []func(*message.Gift) giftHandlers []func(*message.Gift)
guardBuyHandlers []func(*message.GuardBuy) guardBuyHandlers []func(*message.GuardBuy)
liveHandlers []func(*message.Live) liveStartHandlers []func(start *message.LiveStart)
liveStopHandlers []func(start *message.LiveStop)
userToastHandlers []func(*message.UserToast) userToastHandlers []func(*message.UserToast)
} }
@@ -61,11 +62,17 @@ func (c *Client) OnGuardBuy(f func(*message.GuardBuy)) {
c.eventHandlers.guardBuyHandlers = append(c.eventHandlers.guardBuyHandlers, f) c.eventHandlers.guardBuyHandlers = append(c.eventHandlers.guardBuyHandlers, f)
} }
// OnLive 添加 开播事件 的处理器 // OnLiveStart 添加 开播事件 的处理器
func (c *Client) OnLive(f func(*message.Live)) { func (c *Client) OnLiveStart(f func(start *message.LiveStart)) {
c.eventHandlers.liveHandlers = append(c.eventHandlers.liveHandlers, f) c.eventHandlers.liveStartHandlers = append(c.eventHandlers.liveStartHandlers, f)
} }
// OnLiveStop 添加 关播事件 的处理器
func (c *Client) OnLiveStop(f func(start *message.LiveStop)) {
c.eventHandlers.liveStopHandlers = append(c.eventHandlers.liveStopHandlers, f)
}
// OnUserToast 添加 UserToast 的处理
// OnUserToast 添加 UserToast 的处理器 // OnUserToast 添加 UserToast 的处理器
func (c *Client) OnUserToast(f func(*message.UserToast)) { func (c *Client) OnUserToast(f func(*message.UserToast)) {
c.eventHandlers.userToastHandlers = append(c.eventHandlers.userToastHandlers, f) c.eventHandlers.userToastHandlers = append(c.eventHandlers.userToastHandlers, f)
@@ -88,36 +95,49 @@ func (c *Client) Handle(p packet.Packet) {
return return
} }
switch cmd { switch cmd {
// 弹幕
case "DANMU_MSG": case "DANMU_MSG":
d := new(message.Danmaku) d := new(message.Danmaku)
d.Parse(p.Body) d.Parse(p.Body)
for _, fn := range c.eventHandlers.danmakuMessageHandlers { for _, fn := range c.eventHandlers.danmakuMessageHandlers {
go cover(func() { fn(d) }) go cover(func() { fn(d) })
} }
// 醒目留言
case "SUPER_CHAT_MESSAGE": case "SUPER_CHAT_MESSAGE":
s := new(message.SuperChat) s := new(message.SuperChat)
s.Parse(p.Body) s.Parse(p.Body)
for _, fn := range c.eventHandlers.superChatHandlers { for _, fn := range c.eventHandlers.superChatHandlers {
go cover(func() { fn(s) }) go cover(func() { fn(s) })
} }
// 礼物
case "SEND_GIFT": case "SEND_GIFT":
g := new(message.Gift) g := new(message.Gift)
g.Parse(p.Body) g.Parse(p.Body)
for _, fn := range c.eventHandlers.giftHandlers { for _, fn := range c.eventHandlers.giftHandlers {
go cover(func() { fn(g) }) go cover(func() { fn(g) })
} }
// 大航海
case "GUARD_BUY": case "GUARD_BUY":
g := new(message.GuardBuy) g := new(message.GuardBuy)
g.Parse(p.Body) g.Parse(p.Body)
for _, fn := range c.eventHandlers.guardBuyHandlers { for _, fn := range c.eventHandlers.guardBuyHandlers {
go cover(func() { fn(g) }) go cover(func() { fn(g) })
} }
// 开播
case "LIVE": case "LIVE":
l := new(message.Live) l := new(message.LiveStart)
l.Parse(p.Body) l.Parse(p.Body)
for _, fn := range c.eventHandlers.liveHandlers { for _, fn := range c.eventHandlers.liveStartHandlers {
go cover(func() { fn(l) }) go cover(func() { fn(l) })
} }
//下播
case "PREPARING":
l := new(message.LiveStop)
l.Parse(p.Body)
for _, fn := range c.eventHandlers.liveStopHandlers {
go cover(func() { fn(l) })
}
// 用户 toast
case "USER_TOAST_MSG": case "USER_TOAST_MSG":
u := new(message.UserToast) u := new(message.UserToast)
u.Parse(p.Body) u.Parse(p.Body)

View File

@@ -13,8 +13,8 @@ import (
func main() { func main() {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
c := client.NewClient(6) c := client.NewClient(23943357)
c.SetCookie("this is a example cookie.") c.SetCookie("")
//弹幕事件 //弹幕事件
c.OnDanmaku(func(danmaku *message.Danmaku) { c.OnDanmaku(func(danmaku *message.Danmaku) {
if danmaku.Type == message.EmoticonDanmaku { if danmaku.Type == message.EmoticonDanmaku {

15
go.mod
View File

@@ -1,14 +1,17 @@
module github.com/AynaLivePlayer/blivedm-go module github.com/AynaLivePlayer/blivedm-go
go 1.16 go 1.20
require ( require (
github.com/andybalholm/brotli v1.0.5 github.com/andybalholm/brotli v1.1.0
github.com/gorilla/websocket v1.5.0 github.com/gorilla/websocket v1.5.3
github.com/sirupsen/logrus v1.9.3 github.com/sirupsen/logrus v1.9.3
github.com/tidwall/gjson v1.16.0 github.com/tidwall/gjson v1.17.3
google.golang.org/protobuf v1.34.2
)
require (
github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect
golang.org/x/sys v0.12.0 // indirect golang.org/x/sys v0.25.0 // indirect
google.golang.org/protobuf v1.31.0
) )

44
go.sum
View File

@@ -1,51 +1,31 @@
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY= github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/gjson v1.13.0 h1:3TFY9yxOQShrvmjdM76K+jc66zJeT6D3/VFFYCGQf7M= github.com/tidwall/gjson v1.17.3 h1:bwWLZU7icoKRG+C+0PNwIKC6FCJO/Q3p2pZvuP0jN94=
github.com/tidwall/gjson v1.13.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/gjson v1.17.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.16.0 h1:SyXa+dsSPpUlcwEDuKuEBJEz5vzTvOea+9rjyYodQFg=
github.com/tidwall/gjson v1.16.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da h1:b3NXsE2LusjYGGjL5bxEVZZORm/YEFFrWFjR8eFrw/c=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -67,6 +67,7 @@ func (d *Danmaku) Parse(data []byte) {
ext := new(Extra) ext := new(Extra)
emo := new(Emoticon) emo := new(Emoticon)
//扩展字段
err := utils.UnmarshalStr(info.Get("0.15.extra").String(), ext) err := utils.UnmarshalStr(info.Get("0.15.extra").String(), ext)
if err != nil { if err != nil {
log.Error("parse danmaku extra failed") log.Error("parse danmaku extra failed")
@@ -77,27 +78,30 @@ func (d *Danmaku) Parse(data []byte) {
} }
i2 := info.Get("2") i2 := info.Get("2")
i3 := info.Get("3") i3 := info.Get("3")
d.Content = info.Get("1").String() d.Content = info.Get("1").String() //弹幕内容
d.Sender = &User{
Uid: int(i2.Get("0").Int()), d.Sender = &User{ //用户信息
Uname: i2.Get("1").String(), Uid: int(i2.Get("0").Int()), //用户uid
Admin: i2.Get("2").Bool(), Uname: i2.Get("1").String(), //用户昵称
UserLevel: i2.Get("16.0").Int(), //用户等级
Admin: i2.Get("2").Bool(), //是否为管理者
Urank: int(i2.Get("5").Int()), Urank: int(i2.Get("5").Int()),
MobileVerify: i2.Get("6").Bool(), MobileVerify: i2.Get("6").Bool(), //是否绑定手机
GuardLevel: int(info.Get("7").Int()), GuardLevel: int(info.Get("7").Int()), //舰队等级
//勋章信息
Medal: &Medal{ Medal: &Medal{
Level: int(i3.Get("0").Int()), Level: int(i3.Get("0").Int()), //勋章等级
Name: i3.Get("1").String(), Name: i3.Get("1").String(), //勋章名称
UpName: i3.Get("2").String(), UpName: i3.Get("2").String(), //勋章上主播昵称
UpRoomId: int(i3.Get("3").Int()), UpRoomId: int(i3.Get("3").Int()), //上主播房间id
Color: int(i3.Get("4").Int()), Color: int(i3.Get("4").Int()), //勋章颜色
UpUid: int(i3.Get("12").Int()), UpUid: int(i3.Get("12").Int()), //上主播uid
}, },
} }
d.Extra = ext d.Extra = ext
d.Emoticon = emo d.Emoticon = emo
d.Type = int(info.Get("0.12").Int()) d.Type = int(info.Get("0.12").Int()) //弹幕类型
d.Timestamp = info.Get("0.4").Int() d.Timestamp = info.Get("0.4").Int() //时间戳
d.Raw = sb d.Raw = sb
dmv2Content := parsed.Get("dm_v2").String() dmv2Content := parsed.Get("dm_v2").String()

View File

@@ -6,109 +6,112 @@ import (
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
) )
// 礼物消息结构体
type Gift struct { type Gift struct {
Action string `json:"action"` Action string `json:"action"` // 操作类型
BatchComboId string `json:"batch_combo_id"` BatchComboId string `json:"batch_combo_id"` // 批量组合ID
BatchComboSend interface{} `json:"batch_combo_send"` BatchComboSend interface{} `json:"batch_combo_send"` // 批量组合发送信息
BeatId string `json:"beatId"` BeatId string `json:"beatId"` // Beat ID
BizSource string `json:"biz_source"` BizSource string `json:"biz_source"` // 业务来源
BlindGift interface{} `json:"blind_gift"` BlindGift interface{} `json:"blind_gift"` // 盲盒礼物信息
BroadcastId int `json:"broadcast_id"` BroadcastId int `json:"broadcast_id"` // 广播ID
CoinType string `json:"coin_type"` CoinType string `json:"coin_type"` // 币种类型
ComboResourcesId int `json:"combo_resources_id"` ComboResourcesId int `json:"combo_resources_id"` // 组合资源ID
ComboSend interface{} `json:"combo_send"` ComboSend interface{} `json:"combo_send"` // 组合发送信息
ComboStayTime int `json:"combo_stay_time"` ComboStayTime int `json:"combo_stay_time"` // 组合停留时间
ComboTotalCoin int `json:"combo_total_coin"` ComboTotalCoin int `json:"combo_total_coin"` // 组合总硬币数
CritProb int `json:"crit_prob"` CritProb int `json:"crit_prob"` // 批判概率
Demarcation int `json:"demarcation"` Demarcation int `json:"demarcation"` // 分界线
DiscountPrice int `json:"discount_price"` DiscountPrice int `json:"discount_price"` // 折扣价格
Dmscore int `json:"dmscore"` Dmscore int `json:"dmscore"` // DM分数
Draw int `json:"draw"` Draw int `json:"draw"` // 抽奖
Effect int `json:"effect"` Effect int `json:"effect"` // 效果
EffectBlock int `json:"effect_block"` EffectBlock int `json:"effect_block"` // 效果块
Face string `json:"face"` Face string `json:"face"` // 头像URL
FloatScResourceId int `json:"float_sc_resource_id"` FloatScResourceId int `json:"float_sc_resource_id"` // 浮动资源ID
GiftId int `json:"giftId"` GiftId int `json:"giftId"` // 礼物ID
GiftName string `json:"giftName"` GiftName string `json:"giftName"` // 礼物名称
GiftType int `json:"giftType"` GiftType int `json:"giftType"` // 礼物类型
Gold int `json:"gold"` Gold int `json:"gold"` // 金币数量
GuardLevel int `json:"guard_level"` GuardLevel int `json:"guard_level"` // 守护等级
IsFirst bool `json:"is_first"` IsFirst bool `json:"is_first"` // 是否首次赠送
IsSpecialBatch int `json:"is_special_batch"` IsSpecialBatch int `json:"is_special_batch"` // 是否特殊批量
Magnification float64 `json:"magnification"` Magnification float64 `json:"magnification"` // 放大倍数
MedalInfo struct { MedalInfo struct {
AnchorRoomid int `json:"anchor_roomid"` AnchorRoomid int `json:"anchor_roomid"` // 主播房间ID
AnchorUname string `json:"anchor_uname"` AnchorUname string `json:"anchor_uname"` // 主播用户名
GuardLevel int `json:"guard_level"` GuardLevel int `json:"guard_level"` // 守护等级
IconId int `json:"icon_id"` IconId int `json:"icon_id"` // 图标ID
IsLighted int `json:"is_lighted"` IsLighted int `json:"is_lighted"` // 是否点亮
MedalColor int `json:"medal_color"` MedalColor int `json:"medal_color"` // 勋章颜色
MedalColorBorder int `json:"medal_color_border"` MedalColorBorder int `json:"medal_color_border"` // 勋章边框颜色
MedalColorEnd int `json:"medal_color_end"` MedalColorEnd int `json:"medal_color_end"` // 勋章结束颜色
MedalColorStart int `json:"medal_color_start"` MedalColorStart int `json:"medal_color_start"` // 勋章开始颜色
MedalLevel int `json:"medal_level"` MedalLevel int `json:"medal_level"` // 勋章等级
MedalName string `json:"medal_name"` MedalName string `json:"medal_name"` // 勋章名称
Special string `json:"special"` Special string `json:"special"` // 特殊标记
TargetId int `json:"target_id"` TargetId int `json:"target_id"` // 目标ID
} `json:"medal_info"` } `json:"medal_info"`
NameColor string `json:"name_color"` NameColor string `json:"name_color"` // 名称颜色
Num int `json:"num"` Num int `json:"num"` // 数量
OriginalGiftName string `json:"original_gift_name"` OriginalGiftName string `json:"original_gift_name"` // 原始礼物名称
Price int `json:"price"` Price int `json:"price"` // 价格
Rcost int `json:"rcost"` Rcost int `json:"rcost"` // 实际花费
Remain int `json:"remain"` Remain int `json:"remain"` // 剩余数量
Rnd string `json:"rnd"` Rnd string `json:"rnd"` // 随机数
SendMaster interface{} `json:"send_master"` SendMaster interface{} `json:"send_master"` // 发送者信息
Silver int `json:"silver"` Silver int `json:"silver"` // 银币数量
Super int `json:"super"` Super int `json:"super"` // 是否超级礼物
SuperBatchGiftNum int `json:"super_batch_gift_num"` SuperBatchGiftNum int `json:"super_batch_gift_num"` // 超级批量礼物数量
SuperGiftNum int `json:"super_gift_num"` SuperGiftNum int `json:"super_gift_num"` // 超级礼物数量
SvgaBlock int `json:"svga_block"` SvgaBlock int `json:"svga_block"` // SVGA块
TagImage string `json:"tag_image"` TagImage string `json:"tag_image"` // 标签图片URL
Tid string `json:"tid"` Tid string `json:"tid"` // TID
Timestamp int `json:"timestamp"` Timestamp int `json:"timestamp"` // 时间戳
TopList interface{} `json:"top_list"` TopList interface{} `json:"top_list"` // 顶级列表
TotalCoin int `json:"total_coin"` TotalCoin int `json:"total_coin"` // 总硬币数
Uid int `json:"uid"` Uid int `json:"uid"` // 用户ID
Uname string `json:"uname"` Uname string `json:"uname"` // 用户名
} }
// 组合发送结构体
type ComboSend struct { type ComboSend struct {
Action string `json:"action"` Action string `json:"action"` // 操作类型
BatchComboId string `json:"batch_combo_id"` BatchComboId string `json:"batch_combo_id"` // 批量组合ID
BatchComboNum int `json:"batch_combo_num"` BatchComboNum int `json:"batch_combo_num"` // 批量组合数量
ComboId string `json:"combo_id"` ComboId string `json:"combo_id"` // 组合ID
ComboNum int `json:"combo_num"` ComboNum int `json:"combo_num"` // 组合数量
ComboTotalCoin int `json:"combo_total_coin"` ComboTotalCoin int `json:"combo_total_coin"` // 组合总硬币数
Dmscore int `json:"dmscore"` Dmscore int `json:"dmscore"` // DM分数
GiftId int `json:"gift_id"` GiftId int `json:"gift_id"` // 礼物ID
GiftName string `json:"gift_name"` GiftName string `json:"gift_name"` // 礼物名称
GiftNum int `json:"gift_num"` GiftNum int `json:"gift_num"` // 礼物数量
IsShow int `json:"is_show"` IsShow int `json:"is_show"` // 是否显示
MedalInfo struct { MedalInfo struct {
AnchorRoomid int `json:"anchor_roomid"` AnchorRoomid int `json:"anchor_roomid"` // 主播房间ID
AnchorUname string `json:"anchor_uname"` AnchorUname string `json:"anchor_uname"` // 主播用户名
GuardLevel int `json:"guard_level"` GuardLevel int `json:"guard_level"` // 守护等级
IconId int `json:"icon_id"` IconId int `json:"icon_id"` // 图标ID
IsLighted int `json:"is_lighted"` IsLighted int `json:"is_lighted"` // 是否点亮
MedalColor int `json:"medal_color"` MedalColor int `json:"medal_color"` // 勋章颜色
MedalColorBorder int `json:"medal_color_border"` MedalColorBorder int `json:"medal_color_border"` // 勋章边框颜色
MedalColorEnd int `json:"medal_color_end"` MedalColorEnd int `json:"medal_color_end"` // 勋章结束颜色
MedalColorStart int `json:"medal_color_start"` MedalColorStart int `json:"medal_color_start"` // 勋章开始颜色
MedalLevel int `json:"medal_level"` MedalLevel int `json:"medal_level"` // 勋章等级
MedalName string `json:"medal_name"` MedalName string `json:"medal_name"` // 勋章名称
Special string `json:"special"` Special string `json:"special"` // 特殊标记
TargetId int `json:"target_id"` TargetId int `json:"target_id"` // 目标ID
} `json:"medal_info"` } `json:"medal_info"`
NameColor string `json:"name_color"` NameColor string `json:"name_color"` // 名称颜色
RUname string `json:"r_uname"` RUname string `json:"r_uname"` // 接收者用户名
Ruid int `json:"ruid"` Ruid int `json:"ruid"` // 接收者用户ID
SendMaster interface{} `json:"send_master"` SendMaster interface{} `json:"send_master"` // 发送者信息
TotalNum int `json:"total_num"` TotalNum int `json:"total_num"` // 总数量
Uid int `json:"uid"` Uid int `json:"uid"` // 用户ID
Uname string `json:"uname"` Uname string `json:"uname"` // 用户名
} }
// 解析礼物消息数据
func (g *Gift) Parse(data []byte) { func (g *Gift) Parse(data []byte) {
sb := utils.BytesToString(data) sb := utils.BytesToString(data)
sd := gjson.Get(sb, "data").String() sd := gjson.Get(sb, "data").String()

View File

@@ -6,16 +6,17 @@ import (
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
) )
// GuardBuy 购买守护消息结构体
type GuardBuy struct { type GuardBuy struct {
Uid int `json:"uid"` Uid int `json:"uid"` // 用户ID
Username string `json:"username"` Username string `json:"username"` // 用户名
GuardLevel int `json:"guard_level"` GuardLevel int `json:"guard_level"` // 守护等级
Num int `json:"num"` Num int `json:"num"` // 数量
Price int `json:"price"` Price int `json:"price"` // 价格
GiftId int `json:"gift_id"` GiftId int `json:"gift_id"` // 礼物ID
GiftName string `json:"gift_name"` GiftName string `json:"gift_name"` // 礼物名称
StartTime int `json:"start_time"` StartTime int `json:"start_time"` // 开始时间戳
EndTime int `json:"end_time"` EndTime int `json:"end_time"` // 结束时间戳
} }
func (g *GuardBuy) Parse(data []byte) { func (g *GuardBuy) Parse(data []byte) {

View File

@@ -5,27 +5,47 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
// StopLiveRoomList 停止直播房间列表结构体
type StopLiveRoomList struct { type StopLiveRoomList struct {
RoomIdList []int `json:"room_id_list"` RoomIdList []int `json:"room_id_list"` // 房间ID列表
} }
type Live struct { // Live 直播消息结构体
Cmd string `json:"cmd"` type LiveStart struct {
LiveKey string `json:"live_key"` Cmd string `json:"cmd"` // 命令
VoiceBackground string `json:"voice_background"` LiveKey string `json:"live_key"` // 直播密钥
SubSessionKey string `json:"sub_session_key"` VoiceBackground string `json:"voice_background"` // 语音背景
LivePlatform string `json:"live_platform"` SubSessionKey string `json:"sub_session_key"` // 子会话密钥
LiveModel int `json:"live_model"` LivePlatform string `json:"live_platform"` // 直播平台
LiveTime int `json:"live_time"` LiveModel int `json:"live_model"` // 直播模式
Roomid int `json:"roomid"` LiveTime int `json:"live_time"` // 直播时间
Roomid int `json:"roomid"` // 房间ID
} }
// 停止直播
type LiveStop struct {
Cmd string `json:"cmd"`
MsgId string `json:"msg_id"`
PIsAck bool `json:"p_is_ack"`
PMsgType int `json:"p_msg_type"`
Roomid string `json:"roomid"`
Round int `json:"round"` //开启轮播时存在,轮播状态: 1正在轮播 0未轮播
SendTime int64 `json:"send_time"`
}
// Preparing 直播准备中消息结构体
type Preparing struct { type Preparing struct {
Cmd string `json:"cmd"` Cmd string `json:"cmd"` // 命令
Roomid string `json:"roomid"` Roomid string `json:"roomid"` // 房间ID
} }
func (l *Live) Parse(data []byte) { func (l *LiveStart) Parse(data []byte) {
err := json.Unmarshal(data, l)
if err != nil {
log.Error("parse live failed")
}
}
func (l *LiveStop) Parse(data []byte) {
err := json.Unmarshal(data, l) err := json.Unmarshal(data, l)
if err != nil { if err != nil {
log.Error("parse live failed") log.Error("parse live failed")

View File

@@ -6,7 +6,7 @@ import (
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
) )
// SuperChat // SuperChat 超级弹幕消息结构体
// message_jpn: 消息日文翻译目前只出现在SUPER_CHAT_MESSAGE_JPN // message_jpn: 消息日文翻译目前只出现在SUPER_CHAT_MESSAGE_JPN
// id_: str消息ID删除时用 // id_: str消息ID删除时用
type SuperChat struct { type SuperChat struct {

View File

@@ -8,6 +8,7 @@ type User struct {
MobileVerify bool MobileVerify bool
Medal *Medal Medal *Medal
GuardLevel int GuardLevel int
UserLevel int64
} }
type Medal struct { type Medal struct {

View File

@@ -2,6 +2,7 @@ package packet
import ( import (
"encoding/json" "encoding/json"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )

View File

@@ -5,9 +5,10 @@ import (
"compress/zlib" "compress/zlib"
"encoding/binary" "encoding/binary"
"encoding/json" "encoding/json"
"io"
"github.com/andybalholm/brotli" "github.com/andybalholm/brotli"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"io"
) )
const ( const (
@@ -53,14 +54,18 @@ func NewPlainPacket(operation int, body []byte) Packet {
} }
func NewPacketFromBytes(data []byte) Packet { func NewPacketFromBytes(data []byte) Packet {
packLen := binary.BigEndian.Uint32(data[0:4]) if len(data) == 0 {
if packLen == 0 {
return Packet{ProtocolVersion: 99}
}
// 校验包长度
if int(packLen) != len(data) {
log.Error("error packet") log.Error("error packet")
return Packet{ProtocolVersion: 1, Operation: uint32(3), Body: make([]byte, 0)}
} }
packLen := binary.BigEndian.Uint32(data[0:4])
if int(packLen) == 0 || int(packLen) != len(data) {
log.Error("error packet")
return Packet{ProtocolVersion: 1, Operation: uint32(3), Body: make([]byte, 0)}
}
pv := binary.BigEndian.Uint16(data[6:8]) pv := binary.BigEndian.Uint16(data[6:8])
op := binary.BigEndian.Uint32(data[8:12]) op := binary.BigEndian.Uint32(data[8:12])
body := data[16:packLen] body := data[16:packLen]