diff --git a/httpd/wcfrest/receiver.go b/httpd/wcfrest/receiver_url.go similarity index 50% rename from httpd/wcfrest/receiver.go rename to httpd/wcfrest/receiver_url.go index 51371396..ae210760 100644 --- a/httpd/wcfrest/receiver.go +++ b/httpd/wcfrest/receiver_url.go @@ -3,7 +3,6 @@ package wcfrest import ( "errors" - "github.com/gorilla/websocket" "github.com/opentdp/go-helper/logman" "github.com/opentdp/go-helper/request" @@ -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) @@ -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 - -} diff --git a/httpd/wcfrest/receiver_ws.go b/httpd/wcfrest/receiver_ws.go new file mode 100644 index 00000000..0836498e --- /dev/null +++ b/httpd/wcfrest/receiver_ws.go @@ -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 + +} diff --git a/wclient/robot/receiver.go b/wclient/robot/receiver.go index 744703cd..7bc156ad 100644 --- a/wclient/robot/receiver.go +++ b/wclient/robot/receiver.go @@ -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)