-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
163 lines (138 loc) · 2.99 KB
/
client.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package webreal
import (
"encoding/json"
"github.com/gorilla/websocket"
uuid "github.com/satori/go.uuid"
"net/http"
"net/url"
"sync"
"time"
)
var (
newline = []byte{'\n'}
)
// 一个连接一个Client,负责处理连接的I/O
type Client struct {
id string
writeChan chan []byte
conn *websocket.Conn
handler Handler
hub *SubscriptionHub
req *http.Request
mu sync.Mutex
channels map[string]struct{}
conf *Config
ctx sync.Map
}
func newClient(conn *websocket.Conn, handler Handler, hub *SubscriptionHub, req *http.Request, c *Config) *Client {
return &Client{
id: uuid.NewV4().String(),
writeChan: make(chan []byte, c.WriteBufferSize),
conn: conn,
handler: handler,
hub: hub,
channels: map[string]struct{}{},
req: req,
conf: c,
ctx: sync.Map{},
}
}
func (c *Client) run() {
go c.reader()
go c.writer()
c.handler.OnConnect(c)
}
// 读取客户端发过来的内容
func (c *Client) reader() {
defer func() {
c.close()
c.handler.OnClose(c)
}()
c.conn.SetReadLimit(c.conf.MaxMessageSize)
c.conn.SetReadDeadline(time.Now().Add(c.conf.PongWait))
c.conn.SetPongHandler(func(string) error { return c.conn.SetReadDeadline(time.Now().Add(c.conf.PongWait)) })
for {
_, buf, err := c.conn.ReadMessage()
if err != nil {
return
}
var msg Message
if err = json.Unmarshal(buf, &msg); err != nil {
return
}
c.handler.OnMessage(c, &msg)
}
}
// 向客户端写入内容
func (c *Client) writer() {
tik := time.NewTicker(c.conf.PingInterval)
defer func() {
c.close()
tik.Stop()
c.conn.Close()
}()
for {
select {
case buf, _ := <-c.writeChan:
c.conn.SetWriteDeadline(time.Now().Add(c.conf.WriteWait))
if err := c.conn.WriteMessage(websocket.TextMessage, buf); err != nil {
return
}
case <-tik.C:
c.conn.SetWriteDeadline(time.Now().Add(c.conf.WriteWait))
if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil {
return
}
}
}
}
// 关闭
func (c *Client) close() {
c.UnsubscribeAll()
}
// 订阅
func (c *Client) Subscribe(channel string) {
c.mu.Lock()
defer c.mu.Unlock()
if _, found := c.channels[channel]; found {
return
}
c.hub.Subscribe(channel, c)
c.channels[channel] = struct{}{}
}
// 退订
func (c *Client) Unsubscribe(channel string) {
c.mu.Lock()
defer c.mu.Unlock()
c.hub.Unsubscribe(channel, c)
delete(c.channels, channel)
}
// 退订所有
func (c *Client) UnsubscribeAll() {
c.mu.Lock()
defer c.mu.Unlock()
for channel := range c.channels {
c.hub.Unsubscribe(channel, c)
}
c.channels = map[string]struct{}{}
}
// 获取客户端ID
func (c *Client) ID() string {
return c.id
}
// 向客户端发送数据
func (c *Client) Write(d []byte) {
c.writeChan <- d
}
// 获取连接参数
func (c *Client) Query() url.Values {
return c.req.URL.Query()
}
// 获取请求对象
func (c *Client) Request() *http.Request {
return c.req
}
// 关闭连接对象
func (c *Client) Close() error {
return c.conn.Close()
}