Protect ws writing in Gorilla ws

This commit is contained in:
Zoltan Papp 2024-06-07 16:07:35 +02:00
parent 1e115e3893
commit ed8def4d9b

View File

@ -3,6 +3,7 @@ package ws
import ( import (
"fmt" "fmt"
"net" "net"
"sync"
"time" "time"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
@ -10,11 +11,12 @@ import (
type Conn struct { type Conn struct {
*websocket.Conn *websocket.Conn
mu sync.Mutex
} }
func NewConn(wsConn *websocket.Conn) net.Conn { func NewConn(wsConn *websocket.Conn) net.Conn {
return &Conn{ return &Conn{
wsConn, Conn: wsConn,
} }
} }
@ -32,7 +34,9 @@ func (c *Conn) Read(b []byte) (n int, err error) {
} }
func (c *Conn) Write(b []byte) (int, error) { func (c *Conn) Write(b []byte) (int, error) {
c.mu.Lock()
err := c.WriteMessage(websocket.BinaryMessage, b) err := c.WriteMessage(websocket.BinaryMessage, b)
c.mu.Unlock()
return len(b), err return len(b), err
} }