From 16da554a6078d8a8efc3c975acd53295a11c72b5 Mon Sep 17 00:00:00 2001 From: ayflying Date: Mon, 21 Apr 2025 14:35:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84weboscket=E7=9A=84=E5=90=AF?= =?UTF-8?q?=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/websocket/socket_new.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/websocket/socket_new.go b/pkg/websocket/socket_new.go index a9d8368..b9f22b2 100644 --- a/pkg/websocket/socket_new.go +++ b/pkg/websocket/socket_new.go @@ -14,6 +14,7 @@ import ( "github.com/gogf/gf/v2/util/guid" "github.com/gorilla/websocket" "google.golang.org/protobuf/proto" + "net/http" "sync" "time" ) @@ -57,14 +58,21 @@ type SocketInterface interface { func (s *SocketV1) Load(serv *ghttp.Server, prefix string) { //websocket服务启动 serv.Group(prefix, func(group *ghttp.RouterGroup) { - var websocketCfg = websocket.Upgrader{ ReadBufferSize: 1024, WriteBufferSize: 1024, + CheckOrigin: func(r *http.Request) bool { + // In production, you should implement proper origin checking + return true + }, + Error: func(w http.ResponseWriter, r *http.Request, status int, reason error) { + g.Log().Errorf(r.Context(), "WebSocket error: %v", reason) + }, } group.Bind( func(r *ghttp.Request) { ctx := r.Context() + ws, err := websocketCfg.Upgrade(r.Response.Writer, r.Request, nil) if err != nil { glog.Error(ctx, err)