Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[部分破坏性更新] 修复部分问题 & 修改client逻辑 & 添加函数 #21

Closed
wants to merge 10 commits into from
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@
# Go workspace file
go.work

build/*
build/*

.idea/
8 changes: 0 additions & 8 deletions .idea/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion .idea/.name

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/bilichat_core.iml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ func main() {
}
```

#### 如果你有删除房间或debug的需求, 可以绑定函数时传入函数的名称
```go
h.AddOption(handle.CmdDanmuMsg, 26097368, func(event handle.MsgEvent) {
fmt.Printf("[%v] %v: %v\n", event.RoomId, event.DanMuMsg.Data.Sender.Name, event.DanMuMsg.Data.Content)
}, "弹幕")
h.DelOption("弹幕")
```

**关于为什么在处理绑定函数时, 多一个直播间号的参数, 因为考虑到可能会有根据不同的直播间分发处理消息的需求**

### 对于接入的服务器: 程序提供了3个模式:
Expand Down
30 changes: 5 additions & 25 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strconv"
"time"
)
import "github.com/gorilla/websocket"
import "github.com/fasthttp/websocket"

type jsonCoder interface {
Unmarshal(data []byte, v interface{}) error
Expand All @@ -22,7 +22,6 @@ type Client struct {
ctx context.Context
cancel context.CancelFunc
connect *websocket.Conn
revMsg chan []byte
}

func (c *Client) biliChatConnect(url string) error {
Expand All @@ -44,7 +43,7 @@ func (c *Client) sendAuthMsg(wsAuthMsg WsAuthMessage) error {
return nil
}

func (c *Client) receiveWsMsg() {
func (c *Client) receiveWsMsg(handler MsgHandler) {
for {
select {
case <-c.ctx.Done():
Expand All @@ -59,9 +58,7 @@ func (c *Client) receiveWsMsg() {
c.Connected = false
c.connectLoop()
}
c.revMsg <- message
} else {
time.Sleep(200 * time.Millisecond)
handler.MsgHandler(message)
}
}
}
Expand All @@ -73,7 +70,7 @@ func (c *Client) heartBeat() {
select {
case <-c.ctx.Done():
log.Debug("heartBeat exit...")
_ = c.connect.Close()
//_ = c.connect.Close()
return
case <-t.C:
if c.Connected && c.connect != nil {
Expand All @@ -85,21 +82,6 @@ func (c *Client) heartBeat() {
}
}

func (c *Client) revHandler(handler MsgHandler) {
for {
select {
case <-c.ctx.Done():
log.Debug("revHandler exit...")
c.revMsg = nil
return
case msg, ok := <-c.revMsg:
if ok {
go handler.MsgHandler(msg)
}
}
}
}

func (c *Client) sendConnect() error {
wsAuthMsg := WsAuthMessage{Body: WsAuthBody{UID: 0, Roomid: c.RoomId, Protover: 3, Platform: "web", Type: 2}}
// No CDN Mode
Expand Down Expand Up @@ -181,11 +163,9 @@ func (c *Client) BiliChat(CmdChan chan map[string]interface{}) {
}
}()
c.connectLoop()
c.revMsg = make(chan []byte, 10)
handler := MsgHandler{RoomId: c.RoomId, CmdChan: CmdChan}
c.ctx, c.cancel = context.WithCancel(context.Background())
go c.revHandler(handler)
go c.receiveWsMsg()
go c.receiveWsMsg(handler)
go c.heartBeat()
log.Debug("start blive success: ", c.RoomId)
}
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ go 1.19
require (
github.com/andybalholm/brotli v1.0.4
github.com/bytedance/sonic v1.8.3
github.com/fasthttp/websocket v1.5.1
github.com/go-ping/ping v1.1.0
github.com/gorilla/websocket v1.5.0
github.com/sirupsen/logrus v1.9.0
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816
)

require (
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d // indirect
github.com/stretchr/testify v1.8.2 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.44.0 // indirect
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sync v0.1.0 // indirect
Expand Down
25 changes: 22 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583j
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/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fasthttp/websocket v1.5.1 h1:iZsMv5OtZ1E52hhCnlOm/feLCrPhutlrZgvEGcZa1FM=
github.com/fasthttp/websocket v1.5.1/go.mod h1:s+gJkEn38QXLkNfOe/n75Yb8we+VEho1vYqeUYheomw=
github.com/go-ping/ping v1.1.0 h1:3MCGhVX4fyEUuhsfwPrsEdQw6xspHkv5zHsiSoDFZYw=
github.com/go-ping/ping v1.1.0/go.mod h1:xIFjORFzTxqIV/tDVGO4eDy/bLuSyawEeojSm3GfRGk=
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
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/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY=
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
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/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d h1:Q+gqLBOPkFGHyCJxXMRqtUgUbTjI8/Ze8vu8GGyNFwo=
github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d/go.mod h1:Gy+0tqhJvgGlqnTF8CVGP0AaGRjwBtXs/a5PA0Y3+A4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
Expand All @@ -38,9 +43,17 @@ github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 h1
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816/go.mod h1:tzym/CEb5jnFI+Q0k4Qq3+LvRF4gO3E2pxS8fHP8jcA=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.44.0 h1:R+gLUhldIsfg1HokMuQjdQ5bh9nuXHPIfvkYUu9eR5Q=
github.com/valyala/fasthttp v1.44.0/go.mod h1:f6VbjjoI3z1NDOZOv17o6RvtRSWxC77seBFc2uWtgiY=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 h1:18EFjUmQOcUvxNYSkA6jO9VAiXCnxFY6NyDX0bHDmkU=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220906165146-f3363e06e74c/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -49,11 +62,17 @@ golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
16 changes: 16 additions & 0 deletions handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ func (handler *Handler) DelRoomOption(roomId int) {
}
}

func (handler *Handler) DelOption(name string) {
for k, v := range handler.DoFunc {
for k1, v1 := range v {
for i, v2 := range v1 {
if name == handler.funcNames[fmt.Sprintf("%p", v2)] {
handler.DoFunc[k][k1] = append(handler.DoFunc[k][k1][:i], handler.DoFunc[k][k1][i+1:]...)
if len(handler.DoFunc[k][k1]) == 0 {
delete(handler.DoFunc[k], k1)
}
log.Debug("Del Option: ", k, k1, name)
}
}
}
}
}

func (handler *Handler) CmdHandler() {
for {
select {
Expand Down
2 changes: 1 addition & 1 deletion handler/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ type TradingScore struct {

type Preparing struct {
Cmd string `json:"cmd"`
RoomId string `json:"roomid"`
RoomId int `json:"roomid"`
}

type GuardBuy struct {
Expand Down
2 changes: 1 addition & 1 deletion handler/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func (_ *Handler) SetPreparing(msg map[string]interface{}) (m MsgEvent) {
preparing.Cmd = CmdPreparing
tmp := make(map[string]interface{})
if err := JsonCoder.Unmarshal([]byte(msg["msg"].(string)), &tmp); err == nil {
preparing.RoomId = msg["RoomId"].(string)
preparing.RoomId = msg["RoomId"].(int)
m = MsgEvent{Cmd: CmdPreparing, Preparing: &preparing, RoomId: msg["RoomId"].(int)}
}
return
Expand Down
17 changes: 15 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,22 @@ func SetClientPriorityMode(mode int) {
client.ChangeSequenceMode(mode)
}

func GetNewHandler() Handler {
func GetNewHandler() *Handler {
h := Handler{}
h.Handler.DoFunc = make(map[string]map[int][]func(event handler.MsgEvent), 0)
h.Handler.CmdChan = make(chan map[string]interface{}, 10)
h.Handler.Init()
return h
return &h
}

func (h *Handler) AddOption(Cmd string, RoomId int, Do func(event handler.MsgEvent), funcName ...string) {
h.Handler.AddOption(Cmd, RoomId, Do, funcName...)
}

func (h *Handler) DelOption(name string) {
h.Handler.DelOption(name)
}

func (h *Handler) AddRoom(roomId int) error {
if _, ok := h.rooms.Load(roomId); ok {
return RoomAlreadyExist
Expand Down Expand Up @@ -94,6 +98,15 @@ func (h *Handler) DelRoom(RoomId int) error {
return nil
}

func (h *Handler) CountRoom() int {
count := 0
h.rooms.Range(func(key, value interface{}) bool {
count++
return true
})
return count
}

func (h *Handler) Run() {
h.Handler.CmdHandler()
}