Skip to content

Commit

Permalink
fix: 消息监听跨域问题
Browse files Browse the repository at this point in the history
  • Loading branch information
rehiy committed Mar 4, 2024
1 parent 4ef21e0 commit 796daa6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
17 changes: 13 additions & 4 deletions httpd/wcfrest/controller.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package wcfrest

import (
"net/http"
"strings"

"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/mitchellh/mapstructure"
"github.com/opentdp/go-helper/logman"

"github.com/opentdp/wechat-rest/wcferry"
)
Expand All @@ -24,8 +26,14 @@ type CommonPayload struct {
Error error `json:"error,omitempty"`
}

// Websocket 升级
var wsUpgrader = websocket.Upgrader{}
// WS 协议升级
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}

// @Summary 登录二维码
// @Produce json
Expand Down Expand Up @@ -915,8 +923,9 @@ func (wc *Controller) disableReceiver(c *gin.Context) {
// @Router /wcf/socket_receiver [get]
func (wc *Controller) socketReceiver(c *gin.Context) {

ws, err := wsUpgrader.Upgrade(c.Writer, c.Request, nil)
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
logman.Error("websocket upgrade", "error", err)
c.Set("Error", err)
return
}
Expand All @@ -930,6 +939,6 @@ func (wc *Controller) socketReceiver(c *gin.Context) {
}
wc.disableSocketReceiver(ws)

c.Set("Payload", "连接已关闭")
logman.Info("websocket closed", "addr", ws.RemoteAddr())

}
4 changes: 2 additions & 2 deletions httpd/wcfrest/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (wc *Controller) enableSocketReceiver(ws *websocket.Conn) error {
key, err := wc.EnrollReceiver(true, func(msg *wcferry.WxMsg) {
ret := wcferry.ParseWxMsg(msg)
for s := range socketReceiverList {
logman.Info("call receiver", "socket", s.RemoteAddr().String(), "Id", ret.Id)
logman.Info("call receiver", "addr", s.RemoteAddr(), "Id", ret.Id)
go s.WriteJSON(ret)
}
})
Expand All @@ -89,7 +89,7 @@ func (wc *Controller) enableSocketReceiver(ws *websocket.Conn) error {

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

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

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

0 comments on commit 796daa6

Please sign in to comment.