package connection import ( "sync" "github.com/gorilla/websocket" ) type Connection struct { Identificator string 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() }