From: qydysky <32743305+qydysky@users.noreply.github.com> Date: Tue, 31 Jan 2023 12:28:13 +0000 (+0800) Subject: Fix ws不并发写入 X-Git-Tag: v0.21.13 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=01ebd8e2c39322d7266aef282c69a52ca0f1acdc;p=part%2F.git Fix ws不并发写入 --- diff --git a/websocket/Server.go b/websocket/Server.go index 738938a..cef76fc 100644 --- a/websocket/Server.go +++ b/websocket/Server.go @@ -3,6 +3,7 @@ package part import ( "net" "net/http" + "sync" "time" "github.com/gorilla/websocket" @@ -13,6 +14,7 @@ import ( type Server struct { ws_mq *mq.Msgq userpool *idpool.Idpool + m sync.Mutex } type Uinterface struct { @@ -50,6 +52,8 @@ func (t *Server) WS(w http.ResponseWriter, r *http.Request) (o chan uintptr) { t.ws_mq.Pull_tag(map[string]func(interface{}) bool{ `send`: func(data interface{}) bool { if u, ok := data.(Uinterface); ok && u.Id == 0 || u.Id == User.Id { + t.m.Lock() + defer t.m.Unlock() if err := ws.WriteMessage(websocket.TextMessage, u.Data); err != nil { t.ws_mq.Push_tag(`error`, err) return true