-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
87 lines (74 loc) · 1.66 KB
/
conn.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package wspool
import (
"errors"
"github.com/gorilla/websocket"
"sync"
"time"
)
// WsConn is an acquired conn from a Pool.
type WsConn struct {
c *websocket.Conn
p *Pool
mu sync.Mutex
createdAt time.Time
lastUsedAt time.Time
}
// SendMessage sends a message over the WebSocket connection.
func (w *WsConn) SendMessage(message string) error {
if w.c == nil {
return errors.New("connection is nil")
}
w.mu.Lock()
defer w.mu.Unlock()
w.lastUsedAt = time.Now()
return w.c.WriteMessage(websocket.TextMessage, []byte(message))
}
// SendJSON sends a JSON-encoded message over the WebSocket connection.
func (w *WsConn) SendJSON(v interface{}) error {
if w.c == nil {
return errors.New("connection is nil")
}
w.mu.Lock()
defer w.mu.Unlock()
w.lastUsedAt = time.Now()
return w.c.WriteJSON(v)
}
// ReadMessage reads a response from the WebSocket connection.
func (w *WsConn) ReadMessage() ([]byte, error) {
w.mu.Lock()
defer w.mu.Unlock()
_, data, err := w.c.ReadMessage()
if err != nil {
return nil, err
}
return data, nil
}
// ReadJson reads a response as Json from the WebSocket connection and store in v.
func (w *WsConn) ReadJson(v interface{}) error {
w.mu.Lock()
defer w.mu.Unlock()
err := w.c.ReadJSON(v)
if err != nil {
return err
}
return nil
}
// Close closes w and removes it from the pool.
func (w *WsConn) Close() error {
if w.c == nil {
return errors.New("connection is nil")
}
w.mu.Lock()
defer w.mu.Unlock()
err := w.c.Close()
w.c = nil
return err
}
// Release returns w to the pool it was acquired from.
func (w *WsConn) Release() {
if w.c == nil || w.p == nil {
return
}
w.p.release(w)
w.c = nil
}