mirror of
https://github.com/AynaLivePlayer/liveroom-sdk.git
synced 2026-05-09 00:34:28 +08:00
fix bug.
This commit is contained in:
11
api.go
Normal file
11
api.go
Normal file
@@ -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)
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
2
go.mod
2
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
|
||||
)
|
||||
|
||||
18
liveroom.go
18
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))
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user