diff --git a/.gitignore b/.gitignore index f5cc71e..865bd71 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,6 @@ # Go workspace file go.work -build/* \ No newline at end of file +build/* + +.idea/ \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 35410ca..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# 默认忽略的文件 -/shelf/ -/workspace.xml -# 基于编辑器的 HTTP 客户端请求 -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/.name b/.idea/.name deleted file mode 100644 index 5a20cdf..0000000 --- a/.idea/.name +++ /dev/null @@ -1 +0,0 @@ -bilichat_core \ No newline at end of file diff --git a/.idea/bilichat_core.iml b/.idea/bilichat_core.iml deleted file mode 100644 index 5e764c4..0000000 --- a/.idea/bilichat_core.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 943d4b1..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/README.md b/README.md index 25995a4..682da09 100644 --- a/README.md +++ b/README.md @@ -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个模式: diff --git a/client/client.go b/client/client.go index a5de0b4..db03684 100644 --- a/client/client.go +++ b/client/client.go @@ -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 @@ -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 { @@ -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(): @@ -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) } } } @@ -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 { @@ -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 @@ -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) } diff --git a/go.mod b/go.mod index 16ccf36..5171f80 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 291c7fc..be30f04 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= diff --git a/handler/main.go b/handler/main.go index d34f467..e96c985 100644 --- a/handler/main.go +++ b/handler/main.go @@ -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 { diff --git a/handler/template.go b/handler/template.go index 192bb3d..bbb2092 100644 --- a/handler/template.go +++ b/handler/template.go @@ -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 { diff --git a/handler/util.go b/handler/util.go index 892a75f..2bc448c 100644 --- a/handler/util.go +++ b/handler/util.go @@ -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 diff --git a/main.go b/main.go index 92e1c09..4d3f4fc 100644 --- a/main.go +++ b/main.go @@ -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 @@ -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() }