2022-01-12 10:54:45 -04:00
|
|
|
package nostr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2023-04-12 12:14:24 -03:00
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
2022-01-12 10:54:45 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Connection struct {
|
|
|
|
socket *websocket.Conn
|
|
|
|
mutex sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConnection(socket *websocket.Conn) *Connection {
|
|
|
|
return &Connection{
|
|
|
|
socket: socket,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 12:14:24 -03:00
|
|
|
func (c *Connection) WriteJSON(v any) error {
|
2022-01-12 10:54:45 -04:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
return c.socket.WriteJSON(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) WriteMessage(messageType int, data []byte) error {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
return c.socket.WriteMessage(messageType, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) Close() error {
|
|
|
|
return c.socket.Close()
|
|
|
|
}
|