From 0605302db68654bba4de443467015cd8bdde81f5 Mon Sep 17 00:00:00 2001 From: ayflying Date: Fri, 18 Apr 2025 18:36:50 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=8E=A5=E5=8F=A3=E7=9A=84?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/websocket/socket_new.go | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/pkg/websocket/socket_new.go b/pkg/websocket/socket_new.go index 1d38208..d12fb18 100644 --- a/pkg/websocket/socket_new.go +++ b/pkg/websocket/socket_new.go @@ -84,19 +84,17 @@ func (s *SocketV1) Load(serv *ghttp.Server, prefix string) { // @Description: // @receiver s // @param conn -func (s *SocketV1) OnConnect(ctx context.Context, conn *websocket.Conn) { - - defer conn.Close() +func (s *SocketV1) OnConnect(ctx context.Context, ws *websocket.Conn) { id := guid.S() - ip := conn.RemoteAddr().String() - data := &WebsocketData{ + ip := ws.RemoteAddr().String() + conn := &WebsocketData{ Uuid: id, - Ws: conn, + Ws: ws, Ctx: ctx, Groups: make([]string, 0), RoomId: -1, } - m.Set(id, data) + m.Set(id, conn) //defer delete(Conn, id) @@ -106,22 +104,23 @@ func (s *SocketV1) OnConnect(ctx context.Context, conn *websocket.Conn) { //用户登录钩子执行 for _, connect := range OnConnectHandlers { - connect(data) + connect(conn) } for { //进入当前连接线程拥堵 - msgType, msg, err := conn.ReadMessage() + msgType, msg, err := ws.ReadMessage() s.Type = msgType if err != nil { //客户端断开返回错误,断开当前连接 //g.Log().Error(ctx, err) break } - s.OnMessage(m.Get(id).(*WebsocketData), msg, msgType) + s.OnMessage(conn, msg, msgType) } //关闭连接触发 - s.OnClose(data) + + s.OnClose(conn) g.Log().Debugf(ctx, "断开连接:uuid=%v,ip=%v", id, ip) }