Skip to content

Commit

Permalink
fix: concurrent write to websocket connection
Browse files Browse the repository at this point in the history
  • Loading branch information
withchao committed Nov 21, 2024
1 parent 8cd1aca commit 2b89def
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/msggateway/long_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package msggateway
import (
"encoding/json"
"net/http"
"sync"
"time"

"github.com/openimsdk/tools/apiresp"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 2b89def

Please sign in to comment.