/
conn_ws.go
76 lines (62 loc) · 1.09 KB
/
conn_ws.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
package fasthttp
import (
"github.com/clubpay/ronykit/kit"
"github.com/clubpay/ronykit/kit/utils"
"github.com/fasthttp/websocket"
)
type wsConn struct {
utils.SpinLock
kv map[string]string
id uint64
clientIP string
c *websocket.Conn
}
var _ kit.Conn = (*wsConn)(nil)
func (w *wsConn) Close() {
w.Lock()
w.c = nil
w.Unlock()
}
func (w *wsConn) ConnID() uint64 {
return w.id
}
func (w *wsConn) ClientIP() string {
return w.clientIP
}
func (w *wsConn) Write(data []byte) (int, error) {
var err error
w.Lock()
if w.c != nil {
err = w.c.WriteMessage(websocket.TextMessage, data)
} else {
err = kit.ErrWriteToClosedConn
}
w.Unlock()
if err != nil {
return 0, err
}
return len(data), nil
}
func (w *wsConn) Stream() bool {
return true
}
func (w *wsConn) Walk(f func(key string, val string) bool) {
w.Lock()
for k, v := range w.kv {
if !f(k, v) {
break
}
}
w.Unlock()
}
func (w *wsConn) Get(key string) string {
w.Lock()
v := w.kv[key]
w.Unlock()
return v
}
func (w *wsConn) Set(key string, val string) {
w.Lock()
w.kv[key] = val
w.Unlock()
}