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 (
"fmt"
"net"
"sync"
"time"
"github.com/gorilla/websocket"
@ -10,11 +11,12 @@ import (
type Conn struct {
*websocket.Conn
mu sync.Mutex
}
func NewConn(wsConn *websocket.Conn) net.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) {
c.mu.Lock()
err := c.WriteMessage(websocket.BinaryMessage, b)
c.mu.Unlock()
return len(b), err
}