forked from nbd-wtf/go-nostr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
connection.go
61 lines (54 loc) · 978 Bytes
/
connection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package nostr
import (
"sync"
"github.com/gorilla/websocket"
)
type Connection struct {
socket *websocket.Conn
mutex sync.Mutex
}
func NewConnection(socket *websocket.Conn) *Connection {
return &Connection{
socket: socket,
}
}
func (c *Connection) WriteJSON(v interface{}) error {
c.mutex.Lock()
defer c.mutex.Unlock()
err := c.socket.WriteJSON(v)
if err != nil {
err2 := c.Close()
if err2 != nil {
return err2
}
return err
}
return nil
}
func (c *Connection) WriteMessage(messageType int, data []byte) error {
c.mutex.Lock()
defer c.mutex.Unlock()
err := c.socket.WriteMessage(messageType, data)
if err != nil {
err2 := c.Close()
if err2 != nil {
return err2
}
return err
}
return nil
}
func (c *Connection) Close() error {
err := c.socket.Close()
if err != nil {
err := c.socket.Close()
if err != nil {
err := c.socket.UnderlyingConn().Close()
if err != nil {
return err
}
}
return err
}
return nil
}