package controllers import ( logger "eta/eta_mini_ht_api/common/component/log" "fmt" beego "github.com/beego/beego/v2/server/web" "github.com/gorilla/websocket" "net/http" "time" ) var upgrader = websocket.Upgrader{ CheckOrigin: func(r *http.Request) bool { fmt.Println(r.Header.Get("Authorization")) return true // 允许跨域连接 }, } type WebSocketController struct { beego.Controller } // Connect ws链接 // @Description 风险测评同步接口 // @Success 200 {object} controllers.BaseResponse // @router /connect [get] func (c *WebSocketController) Connect() { ws, err := upgrader.Upgrade(c.Ctx.ResponseWriter, c.Ctx.Request, nil) test := c.Ctx.Request.Header.Get("Authorization") fmt.Println("发大水范德萨" + test) if err != nil { //beego.Error(err) return } defer ws.Close() for { var msg []byte _, msg, err = ws.ReadMessage() if err != nil { logger.Error("", err) break } // 处理接收到的消息 fmt.Printf("Received: %s\n", msg) // 发送消息回客户端 err = ws.WriteMessage(websocket.TextMessage, []byte("Server: "+string(msg))) if err != nil { //beego.Error(err) break } time.Sleep(time.Second) // 模拟延迟 } }