Исправлена гонка записи в сокет
This commit is contained in:
@@ -1,11 +1,38 @@
|
||||
package connection
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
type Connection struct {
|
||||
Identificator string
|
||||
//Подсоединенный сокет
|
||||
Socket *websocket.Conn
|
||||
Socket *websocket.Conn
|
||||
|
||||
writeMu sync.Mutex
|
||||
closeMu sync.Mutex
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (c *Connection) WriteBinary(payload []byte) error {
|
||||
c.writeMu.Lock()
|
||||
defer c.writeMu.Unlock()
|
||||
return c.Socket.WriteMessage(websocket.BinaryMessage, payload)
|
||||
}
|
||||
|
||||
func (c *Connection) WriteJSON(v any) error {
|
||||
c.writeMu.Lock()
|
||||
defer c.writeMu.Unlock()
|
||||
return c.Socket.WriteJSON(v)
|
||||
}
|
||||
|
||||
func (c *Connection) Close() error {
|
||||
c.closeMu.Lock()
|
||||
defer c.closeMu.Unlock()
|
||||
if c.closed {
|
||||
return nil
|
||||
}
|
||||
c.closed = true
|
||||
return c.Socket.Close()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user