修改websockt 连接问题
This commit is contained in:
@@ -33,14 +33,16 @@ var upgrader = websocket.Upgrader{
|
|||||||
func (*Websocks) Init(ctx *gin.Context) {
|
func (*Websocks) Init(ctx *gin.Context) {
|
||||||
WebsocktFailed := libs.ErrorCode["WebsocktFailed"]
|
WebsocktFailed := libs.ErrorCode["WebsocktFailed"]
|
||||||
// 判断当前连接是否合法
|
// 判断当前连接是否合法
|
||||||
tokenStr := ctx.Request.Header.Get("Sec-WebSocket-Protocol")
|
key := http.CanonicalHeaderKey("sec-websocket-protocol")
|
||||||
if tokenStr == "" {
|
protos := ctx.Request.Header[key]
|
||||||
|
if len(protos) > 0 {
|
||||||
ctx.JSON(http.StatusInternalServerError, gin.H{"code": WebsocktFailed.Code, "data": WebsocktFailed.Data, "msg": WebsocktFailed.Msg})
|
ctx.JSON(http.StatusInternalServerError, gin.H{"code": WebsocktFailed.Code, "data": WebsocktFailed.Data, "msg": WebsocktFailed.Msg})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
client := grpcClient.GetUserClient()
|
client := grpcClient.GetUserClient()
|
||||||
ctx1, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
ctx1, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
tokenStr := protos[0]
|
||||||
TokenStatus, err1 := client.ValidateToken(ctx1, &userpb.ValidateTokenRequest{
|
TokenStatus, err1 := client.ValidateToken(ctx1, &userpb.ValidateTokenRequest{
|
||||||
AccessToken: tokenStr,
|
AccessToken: tokenStr,
|
||||||
})
|
})
|
||||||
@@ -65,9 +67,9 @@ func (*Websocks) Init(ctx *gin.Context) {
|
|||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
// 将保存连接状态
|
// 将保存连接状态
|
||||||
ClientsMu.Lock()
|
// ClientsMu.Lock()
|
||||||
Clients[TokenStatus.UserId] = conn
|
// Clients[TokenStatus.UserId] = conn
|
||||||
ClientsMu.Unlock()
|
// ClientsMu.Unlock()
|
||||||
log.Printf("客户端已连接: %s", conn.RemoteAddr())
|
log.Printf("客户端已连接: %s", conn.RemoteAddr())
|
||||||
for {
|
for {
|
||||||
mt, message, err := conn.ReadMessage()
|
mt, message, err := conn.ReadMessage()
|
||||||
|
|||||||
Reference in New Issue
Block a user