From a37638946926da5700408e685ae21284c3a19b79 Mon Sep 17 00:00:00 2001 From: zyj <18107291228@163.com> Date: Fri, 12 Sep 2025 18:31:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9websockt=20=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/websocks.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/api/websocks.go b/api/websocks.go index 5f813a8..39632b5 100644 --- a/api/websocks.go +++ b/api/websocks.go @@ -33,14 +33,16 @@ var upgrader = websocket.Upgrader{ func (*Websocks) Init(ctx *gin.Context) { WebsocktFailed := libs.ErrorCode["WebsocktFailed"] // 判断当前连接是否合法 - tokenStr := ctx.Request.Header.Get("Sec-WebSocket-Protocol") - if tokenStr == "" { + key := http.CanonicalHeaderKey("sec-websocket-protocol") + protos := ctx.Request.Header[key] + if len(protos) > 0 { ctx.JSON(http.StatusInternalServerError, gin.H{"code": WebsocktFailed.Code, "data": WebsocktFailed.Data, "msg": WebsocktFailed.Msg}) return } client := grpcClient.GetUserClient() ctx1, cancel := context.WithTimeout(context.Background(), time.Second*5) defer cancel() + tokenStr := protos[0] TokenStatus, err1 := client.ValidateToken(ctx1, &userpb.ValidateTokenRequest{ AccessToken: tokenStr, }) @@ -65,9 +67,9 @@ func (*Websocks) Init(ctx *gin.Context) { defer conn.Close() // 将保存连接状态 - ClientsMu.Lock() - Clients[TokenStatus.UserId] = conn - ClientsMu.Unlock() + // ClientsMu.Lock() + // Clients[TokenStatus.UserId] = conn + // ClientsMu.Unlock() log.Printf("客户端已连接: %s", conn.RemoteAddr()) for { mt, message, err := conn.ReadMessage()