Skip to content

Commit

Permalink
fix: 消息过快导致ws断开的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
rehiy committed Mar 6, 2024
1 parent e9c71f4 commit 78c2e75
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 48 deletions.
48 changes: 0 additions & 48 deletions httpd/wcfrest/receiver.go → httpd/wcfrest/receiver_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package wcfrest
import (
"errors"

"github.com/gorilla/websocket"
"github.com/opentdp/go-helper/logman"
"github.com/opentdp/go-helper/request"

Expand All @@ -13,9 +12,6 @@ import (
var urlReceiverKey = ""
var urlReceiverList = map[string]bool{}

var socketReceiverKey = ""
var socketReceiverList = map[*websocket.Conn]bool{}

func (wc *Controller) enableUrlReceiver(url string) error {

logman.Warn("enable receiver", "url", url)
Expand Down Expand Up @@ -59,47 +55,3 @@ func (wc *Controller) disableUrlReceiver(url string) error {
return nil

}

func (wc *Controller) enableSocketReceiver(ws *websocket.Conn) error {

logman.Warn("enable receiver", "socket", ws.RemoteAddr().String())

if socketReceiverList[ws] {
return errors.New("socket already exists")
}

if len(socketReceiverList) == 0 {
key, err := wc.EnrollReceiver(true, func(msg *wcferry.WxMsg) {
ret := wcferry.ParseWxMsg(msg)
for s := range socketReceiverList {
logman.Info("call receiver", "addr", s.RemoteAddr(), "Id", ret.Id)
go s.WriteJSON(ret)
}
})
if err != nil {
return err
}
socketReceiverKey = key
}

socketReceiverList[ws] = true
return nil

}

func (wc *Controller) disableSocketReceiver(ws *websocket.Conn) error {

logman.Warn("disable receiver", "addr", ws.RemoteAddr())

if !socketReceiverList[ws] {
return errors.New("socket not exists")
}

delete(socketReceiverList, ws)
if len(socketReceiverList) == 0 {
return wc.DisableReceiver(socketReceiverKey)
}

return nil

}
71 changes: 71 additions & 0 deletions httpd/wcfrest/receiver_ws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package wcfrest

import (
"errors"
"sync"

"github.com/gorilla/websocket"
"github.com/opentdp/go-helper/logman"

"github.com/opentdp/wechat-rest/wcferry"
)

var socketReceiverKey = ""
var socketReceiverList = map[*websocket.Conn]*WebSocketMutex{}

type WebSocketMutex struct {
*websocket.Conn
mu sync.Mutex
}

func (w *WebSocketMutex) WriteJSON(v any) error {
w.mu.Lock()
defer w.mu.Unlock()
return w.Conn.WriteJSON(v)
}

func (wc *Controller) enableSocketReceiver(ws *websocket.Conn) error {

logman.Warn("enable receiver", "socket", ws.RemoteAddr().String())

if socketReceiverList[ws] != nil {
return errors.New("socket already exists")
}

wm := &WebSocketMutex{ws, sync.Mutex{}}

if len(socketReceiverList) == 0 {
key, err := wc.EnrollReceiver(true, func(msg *wcferry.WxMsg) {
ret := wcferry.ParseWxMsg(msg)
for s := range socketReceiverList {
logman.Info("call receiver", "addr", s.RemoteAddr(), "Id", ret.Id)
go s.WriteJSON(ret)
}
})
if err != nil {
return err
}
socketReceiverKey = key
}

socketReceiverList[ws] = wm
return nil

}

func (wc *Controller) disableSocketReceiver(ws *websocket.Conn) error {

logman.Warn("disable receiver", "addr", ws.RemoteAddr())

if socketReceiverList[ws] == nil {
return errors.New("socket not exists")
}

delete(socketReceiverList, ws)
if len(socketReceiverList) == 0 {
return wc.DisableReceiver(socketReceiverKey)
}

return nil

}
6 changes: 6 additions & 0 deletions wclient/robot/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ func hook10002(msg *wcferry.WxMsg) {
return
}

if revoke.Type == 47 {
output += "\n-------\n一个浪漫无敌的表情"
reply(msg, output)
return
}

output += "\n-------\n暂不支持回显的消息类型"
reply(msg, output)

Expand Down

0 comments on commit 78c2e75

Please sign in to comment.