🐛 fix: anonymous danmu msg

This commit is contained in:
HarryWong
2023-07-08 10:22:49 +08:00
parent f56426ffa7
commit 8bc38ae586
6 changed files with 16 additions and 8 deletions

View File

@@ -33,7 +33,7 @@ import (
func main() { func main() {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
c := client.NewClient("732") c := client.NewClient("732", 194484313) // 732为房间号194484313为UID
//弹幕事件 //弹幕事件
c.OnDanmaku(func(danmaku *message.Danmaku) { c.OnDanmaku(func(danmaku *message.Danmaku) {
if danmaku.Type == message.EmoticonDanmaku { if danmaku.Type == message.EmoticonDanmaku {

View File

@@ -4,16 +4,18 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"strconv"
"time"
"github.com/Akegarasu/blivedm-go/api" "github.com/Akegarasu/blivedm-go/api"
"github.com/Akegarasu/blivedm-go/packet" "github.com/Akegarasu/blivedm-go/packet"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"strconv"
"time"
) )
type Client struct { type Client struct {
conn *websocket.Conn conn *websocket.Conn
uid int
roomID string roomID string
tempID string tempID string
token string token string
@@ -26,10 +28,11 @@ type Client struct {
} }
// NewClient 创建一个新的弹幕 client // NewClient 创建一个新的弹幕 client
func NewClient(roomID string) *Client { func NewClient(roomID string, uid int) *Client {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
return &Client{ return &Client{
tempID: roomID, tempID: roomID,
uid: uid,
eventHandlers: &eventHandlers{}, eventHandlers: &eventHandlers{},
customEventHandlers: &customEventHandlers{}, customEventHandlers: &customEventHandlers{},
done: ctx.Done(), done: ctx.Done(),
@@ -157,7 +160,7 @@ func (c *Client) sendEnterPacket() error {
if err != nil { if err != nil {
return errors.New("error roomID") return errors.New("error roomID")
} }
pkt := packet.NewEnterPacket(0, rid, c.token) pkt := packet.NewEnterPacket(c.uid, rid, c.token)
if err = c.conn.WriteMessage(websocket.BinaryMessage, pkt); err != nil { if err = c.conn.WriteMessage(websocket.BinaryMessage, pkt); err != nil {
return err return err
} }

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"github.com/Akegarasu/blivedm-go/client" "github.com/Akegarasu/blivedm-go/client"
_ "github.com/Akegarasu/blivedm-go/utils" _ "github.com/Akegarasu/blivedm-go/utils"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@@ -9,12 +10,13 @@ import (
) )
const roomId = "8792912" const roomId = "8792912"
const uid = 745493
var dumps = []string{"GUARD_BUY", "USER_TOAST_MSG"} var dumps = []string{"GUARD_BUY", "USER_TOAST_MSG"}
func main() { func main() {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
c := client.NewClient(roomId) c := client.NewClient(roomId, uid)
for _, v := range dumps { for _, v := range dumps {
vv := v vv := v
c.RegisterCustomEventHandler(vv, func(s string) { c.RegisterCustomEventHandler(vv, func(s string) {

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"github.com/Akegarasu/blivedm-go/api" "github.com/Akegarasu/blivedm-go/api"
"github.com/Akegarasu/blivedm-go/client" "github.com/Akegarasu/blivedm-go/client"
"github.com/Akegarasu/blivedm-go/message" "github.com/Akegarasu/blivedm-go/message"
@@ -12,7 +13,7 @@ import (
func main() { func main() {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
c := client.NewClient("732") c := client.NewClient("732", 194484313)
//弹幕事件 //弹幕事件
c.OnDanmaku(func(danmaku *message.Danmaku) { c.OnDanmaku(func(danmaku *message.Danmaku) {
if danmaku.Type == message.EmoticonDanmaku { if danmaku.Type == message.EmoticonDanmaku {

1
go.mod
View File

@@ -7,4 +7,5 @@ require (
github.com/gorilla/websocket v1.4.2 github.com/gorilla/websocket v1.4.2
github.com/sirupsen/logrus v1.8.1 github.com/sirupsen/logrus v1.8.1
github.com/tidwall/gjson v1.13.0 github.com/tidwall/gjson v1.13.0
golang.org/x/sys v0.0.0-20210423082822-04245dca01da // indirect
) )

3
go.sum
View File

@@ -16,5 +16,6 @@ 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 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=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 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=