From 2b89def10ed8f2d3a7b00cdb8d4ae214f70ca69d Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Thu, 21 Nov 2024 17:17:44 +0800 Subject: [PATCH] fix: concurrent write to websocket connection --- internal/msggateway/long_conn.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/msggateway/long_conn.go b/internal/msggateway/long_conn.go index c1b3e27c9..db6c83ffa 100644 --- a/internal/msggateway/long_conn.go +++ b/internal/msggateway/long_conn.go @@ -17,6 +17,7 @@ package msggateway import ( "encoding/json" "net/http" + "sync" "time" "github.com/openimsdk/tools/apiresp" @@ -55,6 +56,7 @@ type GWebSocket struct { conn *websocket.Conn handshakeTimeout time.Duration writeBufferSize int + lock sync.Mutex } func newGWebSocket(protocolType int, handshakeTimeout time.Duration, wbs int) *GWebSocket { @@ -84,6 +86,8 @@ func (d *GWebSocket) GenerateLongConn(w http.ResponseWriter, r *http.Request) er } func (d *GWebSocket) WriteMessage(messageType int, message []byte) error { + d.lock.Lock() + defer d.lock.Unlock() // d.setSendConn(d.conn) return d.conn.WriteMessage(messageType, message) }