From 77ba60717aca4cb03866c1207692e776ea09f762 Mon Sep 17 00:00:00 2001 From: "630892807@qq.com" <630892807@qq.com> Date: Mon, 8 Mar 2021 23:25:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=94=81=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ws/user.go | 6 ++++-- ws/ws.go | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/ws/user.go b/ws/user.go index 7d6c9da..0f80d70 100644 --- a/ws/user.go +++ b/ws/user.go @@ -91,8 +91,8 @@ func OneKefuMessage(toId string, str []byte) { if mKefuConns != nil && ok { for _, kefu := range mKefuConns { log.Println("OneKefuMessage lock") - Mux.Lock() - defer Mux.Unlock() + kefu.Mux.Lock() + defer kefu.Mux.Unlock() log.Println("OneKefuMessage unlock") kefu.Conn.WriteMessage(websocket.TextMessage, str) } @@ -113,6 +113,8 @@ func SendPingToKefuClient() { if kefuConn == nil { continue } + kefuConn.Mux.Lock() + defer kefuConn.Mux.Unlock() err := kefuConn.Conn.WriteMessage(websocket.TextMessage, str) if err == nil { newKefuConns = append(newKefuConns, kefuConn) diff --git a/ws/ws.go b/ws/ws.go index 4af674a..706dc7b 100644 --- a/ws/ws.go +++ b/ws/ws.go @@ -21,12 +21,14 @@ type User struct { Avator string To_id string Role_id string + Mux sync.Mutex } type Message struct { conn *websocket.Conn context *gin.Context content []byte messageType int + Mux sync.Mutex } type TypeMessage struct { Type interface{} `json:"type"` @@ -126,9 +128,9 @@ func WsServerBackend() { Type: "pong", } str, _ := json.Marshal(msg) - Mux.Lock() + message.Mux.Lock() + defer message.Mux.Unlock() conn.WriteMessage(websocket.TextMessage, str) - Mux.Unlock() case "inputing": to := typeMsg.Data.(map[string]interface{})["to"].(string) OneKefuMessage(to, message.content)