This repository has been archived by the owner on Jun 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package bili | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/gaoyanpao/biliLiveHelper" | ||
"sync" | ||
) | ||
|
||
type LiveManager struct { | ||
biliClient map[int]*Client | ||
lock sync.RWMutex | ||
} | ||
|
||
type Client struct { | ||
C *biliLiveHelper.Client | ||
OnDanmu func(ctx *biliLiveHelper.Context) | ||
OnGift func(ctx *biliLiveHelper.Context) | ||
OnWelcome func(ctx *biliLiveHelper.Context) | ||
} | ||
|
||
func NewLiveManager() *LiveManager { | ||
return &LiveManager{biliClient: make(map[int]*Client), lock: sync.RWMutex{}} | ||
} | ||
|
||
func (l *LiveManager) AddClient(RoomId int) (c *Client, e error) { | ||
l.lock.Lock() | ||
defer l.lock.Unlock() | ||
client := biliLiveHelper.NewClient(RoomId) | ||
if client == nil { | ||
e = errors.New("Client Err ") | ||
return | ||
} | ||
client.RegHandleFunc(biliLiveHelper.CmdDanmuMsg, func(ctx *biliLiveHelper.Context) bool { | ||
if c.OnDanmu != nil { | ||
c.OnDanmu(ctx) | ||
} | ||
return false | ||
}) | ||
client.RegHandleFunc(biliLiveHelper.CmdSendGift, func(ctx *biliLiveHelper.Context) bool { | ||
if c.OnGift != nil { | ||
c.OnGift(ctx) | ||
} | ||
return false | ||
}) | ||
client.RegHandleFunc(biliLiveHelper.CmdWelcome, func(ctx *biliLiveHelper.Context) bool { | ||
if c.OnWelcome != nil { | ||
c.OnWelcome(ctx) | ||
} | ||
return false | ||
}) | ||
c = &Client{C: client} | ||
if _, ok := l.biliClient[RoomId]; ok { | ||
e = errors.New("已加入了Room") | ||
return | ||
} | ||
l.biliClient[RoomId] = c | ||
return | ||
} | ||
|
||
func (l *LiveManager) RemoveClient(RoomId int) error { | ||
l.lock.Lock() | ||
defer l.lock.Unlock() | ||
if v, ok := l.biliClient[RoomId]; ok { | ||
if v.C.IsConnected() { | ||
v.ExitRoom() | ||
} | ||
delete(l.biliClient, RoomId) | ||
return nil | ||
} else { | ||
return errors.New("Client不存在! ") | ||
} | ||
} | ||
|
||
func (c *Client) RegisterDanmuFunc(f func(ctx *biliLiveHelper.Context)) { | ||
c.OnDanmu = f | ||
} | ||
|
||
func (c *Client) RegisterGiftFunc(f func(ctx *biliLiveHelper.Context)) { | ||
c.OnGift = f | ||
} | ||
func (c *Client) RegisterWelcomeFunc(f func(ctx *biliLiveHelper.Context)) { | ||
c.OnWelcome = f | ||
} | ||
|
||
func (c *Client) GetRoomInfo() biliLiveHelper.SimpleRoomInfo { | ||
return c.C.SimpleRoomInfo | ||
} | ||
|
||
func (c *Client) ExitRoom() error { | ||
return c.C.Conn.Close() | ||
} | ||
|
||
// Start 阻塞 | ||
func (c *Client) Start() error { | ||
|
||
return c.C.StartListen() | ||
} | ||
func GetLiveStatusString(status int) string { | ||
switch status { | ||
case 0: | ||
return "未开播" | ||
case 1: | ||
return "直播中" | ||
case 2: | ||
return "轮播中" | ||
default: | ||
return fmt.Sprintf("未知状态%d", status) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package bili | ||
|
||
import ( | ||
"github.com/gaoyanpao/biliLiveHelper" | ||
"log" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestLive(t *testing.T) { | ||
client := biliLiveHelper.NewClient(650) | ||
if client == nil { | ||
os.Exit(1) | ||
} | ||
client.PrintRoomInfo() | ||
client.RegHandleFunc(biliLiveHelper.CmdDanmuMsg, func(ctx *biliLiveHelper.Context) bool { | ||
data := ctx.Msg | ||
log.Printf("[弹幕]<%v>%s", data.Get("info").GetIndex(2).GetIndex(1).MustString(), data.Get("info").GetIndex(1).MustString()) | ||
return false | ||
}) | ||
client.RegHandleFunc(biliLiveHelper.CmdWelcome, func(ctx *biliLiveHelper.Context) bool { | ||
data := ctx.Msg | ||
log.Printf("[欢迎]%v", data.Get("data")) | ||
return false | ||
}) | ||
client.RegHandleFunc(biliLiveHelper.CmdSendGift, func(ctx *biliLiveHelper.Context) bool { | ||
data := ctx.Msg | ||
log.Printf("[礼物]%v", data.Get("data")) | ||
return false | ||
}) | ||
client.StartListen() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters