From cb4dc5a579575cb41e8272cfd89dfbd121405a19 Mon Sep 17 00:00:00 2001 From: aynakeya Date: Wed, 10 Apr 2024 00:59:49 -0700 Subject: [PATCH] fix bug. --- api.go | 11 +++++++++ example/openblive/openblive.go | 9 ++++---- go.mod | 2 +- liveroom.go | 18 +++++++-------- provider/openblive/apiclient.go | 3 +++ provider/openblive/openblive.go | 40 +++++++++++++++++++-------------- provider/webdm/web.go | 10 ++++----- 7 files changed, 57 insertions(+), 36 deletions(-) create mode 100644 api.go diff --git a/api.go b/api.go new file mode 100644 index 0000000..e706627 --- /dev/null +++ b/api.go @@ -0,0 +1,11 @@ +package liveroom + +import "errors" + +func CreateLiveRoom(config LiveRoom) (ILiveRoom, error) { + provider, ok := GetProvider(config.Provider) + if !ok { + return nil, errors.New("provider not found") + } + return provider.CreateLiveRoom(config) +} diff --git a/example/openblive/openblive.go b/example/openblive/openblive.go index 0379971..a2f5545 100644 --- a/example/openblive/openblive.go +++ b/example/openblive/openblive.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "github.com/AynaLivePlayer/liveroom-sdk" "github.com/AynaLivePlayer/liveroom-sdk/provider/openblive" "os" "os/signal" @@ -12,8 +13,8 @@ const apiServer = "http://0.0.0.0:9090" func main() { provider := openblive.NewOpenBLiveClientProvider(apiServer, 1661006726438) - room, _ := provider.CreateLiveRoom(liveroom.LiveRoomConfig{ - Room: "YOUR_CLIENT_KEY", + room, _ := provider.CreateLiveRoom(liveroom.LiveRoom{ + Room: "CSMVI59S9HC02", Provider: openblive.ProviderName, }) room.OnMessage(func(msg *liveroom.Message) { @@ -28,14 +29,14 @@ func main() { } }) room.OnDisconnect( - func(liveroom liveroom.LiveRoom) { + func(liveroom liveroom.ILiveRoom) { fmt.Println("Disconnected AAAAAAAa") }) fmt.Println("connect", room.Connect()) quit := make(chan os.Signal) signal.Notify(quit, os.Interrupt, os.Kill) <-quit - fmt.Println("disconnect", room.Disconnect()) + fmt.Println("disconnect", room.Disconnect() == nil) time.Sleep(3 * time.Second) fmt.Println("Bye") } diff --git a/go.mod b/go.mod index ee214ca..f2f1ecd 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.20 require ( github.com/AynaLivePlayer/blivedm-go v0.0.0-20240408074929-6565ab41764b - github.com/aynakeya/open-bilibili-live v0.0.3 + github.com/aynakeya/open-bilibili-live v0.0.5 github.com/go-resty/resty/v2 v2.7.0 github.com/spf13/cast v1.5.1 ) diff --git a/liveroom.go b/liveroom.go index f22bd13..e4c07b3 100644 --- a/liveroom.go +++ b/liveroom.go @@ -1,24 +1,24 @@ package liveroom -type LiveRoomConfig struct { +type LiveRoom struct { Provider string `json:"provider"` // Provider is the name of the live room provider Room string `json:"room"` // RoomID is the unique identifier of the live room } -func (l *LiveRoomConfig) Identifier() string { +func (l *LiveRoom) Identifier() string { return l.Provider + "_" + l.Room } type ILiveRoomProvider interface { GetName() string GetDescription() string - CreateLiveRoom(cfg LiveRoomConfig) (LiveRoom, error) + CreateLiveRoom(cfg LiveRoom) (ILiveRoom, error) } type LiveRoomProvider struct { Name string Description string - Func func(cfg LiveRoomConfig) (LiveRoom, error) + Func func(cfg LiveRoom) (ILiveRoom, error) } func (l *LiveRoomProvider) GetName() string { @@ -29,7 +29,7 @@ func (l *LiveRoomProvider) GetDescription() string { return l.Description } -func (l *LiveRoomProvider) CreateLiveRoom(cfg LiveRoomConfig) (LiveRoom, error) { +func (l *LiveRoomProvider) CreateLiveRoom(cfg LiveRoom) (ILiveRoom, error) { return l.Func(cfg) } @@ -59,12 +59,12 @@ type Message struct { Message string } -type LiveRoom interface { - GetName() string // should return the name of the provider - Config() *LiveRoomConfig // should return mutable model (not a copy) +type ILiveRoom interface { + GetName() string // should return the name of the provider + Config() *LiveRoom // should return mutable model (not a copy) Connect() error Disconnect() error - OnDisconnect(func(liveroom LiveRoom)) + OnDisconnect(func(liveroom ILiveRoom)) OnStatusChange(func(connected bool)) OnMessage(func(msg *Message)) } diff --git a/provider/openblive/apiclient.go b/provider/openblive/apiclient.go index bd5e3c9..acfe9c8 100644 --- a/provider/openblive/apiclient.go +++ b/provider/openblive/apiclient.go @@ -41,6 +41,9 @@ func parseApiResponse(resp *resty.Response, err error) (*openblive.AppStartResul if sceneResp.Code != 0 { return nil, openblive.ErrUnknown.WithDetail(errors.New(sceneResp.Msg)) } + if sceneResp.Data.Error != nil && sceneResp.Data.Error.Code == 0 && sceneResp.Data.Error.Message == "" { + return sceneResp.Data.Result, nil + } return sceneResp.Data.Result, sceneResp.Data.Error } diff --git a/provider/openblive/openblive.go b/provider/openblive/openblive.go index 5a9c06d..19831e1 100644 --- a/provider/openblive/openblive.go +++ b/provider/openblive/openblive.go @@ -12,11 +12,11 @@ import ( const ProviderName = "openblive" type OpenBLiveClient struct { - cfg liveroom.LiveRoomConfig + cfg liveroom.LiveRoom openbliveClient *openblive.BLiveClient conn openblive.BLiveLongConnection onMessage func(msg *liveroom.Message) - onDisconnect func(liveroom liveroom.LiveRoom) + onDisconnect func(liveroom liveroom.ILiveRoom) onStatusChange func(connected bool) } @@ -24,7 +24,7 @@ func NewOpenBLiveClientProvider(apiServer string, appId int64) liveroom.ILiveRoo return &liveroom.LiveRoomProvider{ Name: ProviderName, Description: "open bilibili live protocol. enter client key to connect.", - Func: func(cfg liveroom.LiveRoomConfig) (liveroom.LiveRoom, error) { + Func: func(cfg liveroom.LiveRoom) (liveroom.ILiveRoom, error) { if cfg.Provider != ProviderName { return nil, errors.New("invalid provider name") } @@ -37,14 +37,15 @@ func NewOpenBLiveClientProvider(apiServer string, appId int64) liveroom.ILiveRoo } func (o *OpenBLiveClient) danmuHandler(data openblive.DanmakuData) { - if o.onMessage == nil { + msg := o.onMessage + if msg == nil { return } roomId := strconv.Itoa(data.RoomID) if data.FansMedalName == "" { roomId = "" } - o.onMessage(&liveroom.Message{ + msg(&liveroom.Message{ User: liveroom.User{ Uid: data.OpenID, Username: data.UName, @@ -61,11 +62,11 @@ func (o *OpenBLiveClient) danmuHandler(data openblive.DanmakuData) { } func (o *OpenBLiveClient) disconnectHandler(conn openblive.BLiveLongConnection) { - if o.onStatusChange != nil { - o.onStatusChange(false) + if x := o.onStatusChange; x != nil { + x(false) } - if o.onDisconnect != nil { - o.onDisconnect(o) + if x := o.onDisconnect; x != nil { + x(o) } } @@ -73,7 +74,7 @@ func (o *OpenBLiveClient) GetName() string { return ProviderName } -func (o *OpenBLiveClient) Config() *liveroom.LiveRoomConfig { +func (o *OpenBLiveClient) Config() *liveroom.LiveRoom { return &o.cfg } @@ -87,26 +88,31 @@ func (o *OpenBLiveClient) Connect() error { o.conn.OnDisconnect(o.disconnectHandler) e := o.conn.EstablishConnection(context.Background()) if e == nil { - if o.onStatusChange != nil { - o.onStatusChange(true) + if handler := o.onStatusChange; handler != nil { + handler(true) } } return e } func (o *OpenBLiveClient) Disconnect() error { - _ = o.conn.CloseConnection() - if o.onStatusChange != nil { - o.onStatusChange(false) + if o.conn == nil { + return nil } - return o.openbliveClient.End() + _ = o.conn.CloseConnection() + o.conn = nil + if handler := o.onStatusChange; handler != nil { + handler(false) + } + e := o.openbliveClient.End() + return e } func (o *OpenBLiveClient) OnStatusChange(f func(connected bool)) { o.onStatusChange = f } -func (o *OpenBLiveClient) OnDisconnect(f func(liveroom liveroom.LiveRoom)) { +func (o *OpenBLiveClient) OnDisconnect(f func(liveroom liveroom.ILiveRoom)) { o.onDisconnect = f } diff --git a/provider/webdm/web.go b/provider/webdm/web.go index d592382..39a0be9 100644 --- a/provider/webdm/web.go +++ b/provider/webdm/web.go @@ -13,10 +13,10 @@ import ( const ProviderName = "biliweb" type WebDanmuClient struct { - cfg liveroom.LiveRoomConfig + cfg liveroom.LiveRoom webDmClient *client.Client onMessage func(msg *liveroom.Message) - onDisconnect func(liveroom liveroom.LiveRoom) + onDisconnect func(liveroom liveroom.ILiveRoom) onStatusChange func(connected bool) } @@ -24,7 +24,7 @@ func NewWebDanmuClientProvider(apiServer string) liveroom.ILiveRoomProvider { return &liveroom.LiveRoomProvider{ Name: ProviderName, Description: "default web protocol. enter room id to connect.", - Func: func(cfg liveroom.LiveRoomConfig) (liveroom.LiveRoom, error) { + Func: func(cfg liveroom.LiveRoom) (liveroom.ILiveRoom, error) { if cfg.Provider != ProviderName { return nil, errors.New("invalid provider name") } @@ -46,7 +46,7 @@ func (w *WebDanmuClient) GetName() string { return ProviderName } -func (w *WebDanmuClient) Config() *liveroom.LiveRoomConfig { +func (w *WebDanmuClient) Config() *liveroom.LiveRoom { return &w.cfg } @@ -86,7 +86,7 @@ func (w *WebDanmuClient) Disconnect() error { return nil } -func (w *WebDanmuClient) OnDisconnect(f func(liveroom liveroom.LiveRoom)) { +func (w *WebDanmuClient) OnDisconnect(f func(liveroom liveroom.ILiveRoom)) { w.onDisconnect = f }