-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwebsocket.go
439 lines (367 loc) · 9.02 KB
/
websocket.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// Package websocket implements the WebSocket protocol with additional functions.
/*
Examples
Echo server:
package main
import (
"github.com/pkgz/websocket"
"net/http"
)
func main () {
r := http.NewServeMux()
wsServer := websocket.Start(context.Background())
r.HandleFunc("/ws", wsServer.Handler)
wsServer.On("echo", func(ctx context.Context, c *websocket.Conn, msg *websocket.Message) {
c.Emit("echo", msg.Data)
})
http.ListenAndServe(":8080", r)
}
Websocket with group:
package main
import (
"github.com/pkgz/websocket"
"net/http"
)
func main () {
r := http.NewServeMux()
wsServer := websocket.Start(context.Background())
ch := wsServer.NewChannel("test")
wsServer.OnConnect(func(ctx context.Context, c *websocket.Conn) {
ch.Add(c)
ch.Emit("connection", []byte("new connection come"))
})
r.HandleFunc("/ws", wsServer.Handler)
http.ListenAndServe(":8080", r)
}
*/
package websocket
import (
"context"
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
"io"
"log"
"net/http"
"net/url"
"reflect"
"sync"
)
// Server allows keeping connection list, broadcast channel and callbacks list.
type Server struct {
connections map[*Conn]bool
channels map[string]*Channel
broadcast chan Message
callbacks map[string]HandlerFunc
delChan []chan *Conn
onConnect func(ctx context.Context, c *Conn)
onDisconnect func(ctx context.Context, c *Conn)
onMessage func(ctx context.Context, c *Conn, h ws.Header, b []byte)
done bool
mu sync.RWMutex
}
// Message is a struct for data which sending between application and clients.
// Name using for matching callback function in On function.
// Body will be transformed to byte array and returned to callback.
type Message struct {
Name string `json:"name"`
Data []byte `json:"data"`
}
// HandlerFunc is a type for handle function all function which has callback have this struct
// as first element returns pointer to connection
// its give opportunity to close connection or emit message to exactly this connection.
type HandlerFunc func(ctx context.Context, c *Conn, msg *Message)
// New websocket server handler with the provided options.
func New() *Server {
srv := &Server{
connections: make(map[*Conn]bool),
channels: make(map[string]*Channel),
broadcast: make(chan Message),
callbacks: make(map[string]HandlerFunc),
}
srv.onMessage = func(ctx context.Context, c *Conn, h ws.Header, b []byte) {
_ = c.Write(h, b)
}
return srv
}
// Start instantly create and run websocket server.
func Start(ctx context.Context) *Server {
s := New()
s.Run(ctx)
return s
}
// Run start go routine which listening for channels.
func (s *Server) Run(ctx context.Context) {
go func() {
for {
select {
case msg := <-s.broadcast:
go func() {
s.mu.RLock()
for c := range s.connections {
_ = c.Emit(msg.Name, msg.Data)
}
s.mu.RUnlock()
}()
case <-ctx.Done():
if err := s.Shutdown(); err != nil {
log.Print(err)
}
return
}
}
}()
}
// Shutdown must be called before application died
// its goes throw all connection and closing it
// and stopping all goroutines.
func (s *Server) Shutdown() error {
s.mu.Lock()
defer s.mu.Unlock()
l := len(s.connections)
var wg sync.WaitGroup
wg.Add(l)
for c := range s.connections {
go func(c *Conn) {
if c.conn != nil {
_ = c.Close()
}
wg.Done()
}(c)
}
wg.Wait()
s.done = true
return nil
}
// Handler get upgrade connection to RFC 6455 and starting listener for it.
func (s *Server) Handler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var params url.Values = nil
conn, _, _, err := ws.UpgradeHTTP(r, w)
if err != nil {
log.Printf("websocket: upgrade error %v", err)
return
}
defer func() {
_ = conn.Close()
}()
if r.URL.RawQuery != "" {
params, err = url.ParseQuery(r.URL.RawQuery)
if err != nil {
log.Print(err)
return
}
}
connection := &Conn{
id: uuid(),
params: params,
conn: conn,
done: make(chan bool, 1),
}
connection.startPing()
s.addConn(ctx, connection)
textPending := false
state := ws.StateServerSide
utf8Reader := wsutil.NewUTF8Reader(nil)
cipherReader := wsutil.NewCipherReader(nil, [4]byte{0, 0, 0, 0})
for {
header, _ := ws.ReadHeader(conn)
if err = ws.CheckHeader(header, state); err != nil {
log.Printf("drop ws connection: %v", err)
s.dropConn(ctx, connection)
break
}
cipherReader.Reset(io.LimitReader(conn, header.Length), header.Mask)
var utf8Fin bool
var r io.Reader = cipherReader
switch header.OpCode {
case ws.OpPing:
header.OpCode = ws.OpPong
header.Masked = false
_ = ws.WriteHeader(conn, header)
_, _ = io.CopyN(conn, cipherReader, header.Length)
continue
case ws.OpPong:
_, _ = io.CopyN(io.Discard, conn, header.Length)
continue
case ws.OpClose:
utf8Fin = true
case ws.OpContinuation:
if textPending {
utf8Reader.Source = cipherReader
r = utf8Reader
}
if header.Fin {
state = state.Clear(ws.StateFragmented)
textPending = false
utf8Fin = true
}
case ws.OpText:
utf8Reader.Reset(cipherReader)
r = utf8Reader
if !header.Fin {
state = state.Set(ws.StateFragmented)
textPending = true
} else {
utf8Fin = true
}
case ws.OpBinary:
if !header.Fin {
state = state.Set(ws.StateFragmented)
}
}
payload := make([]byte, header.Length)
_, err = io.ReadFull(r, payload)
if err == nil && utf8Fin && !utf8Reader.Valid() {
err = wsutil.ErrInvalidUTF8
}
if err != nil || header.OpCode == ws.OpClose {
if err != nil {
log.Printf("drop ws connection: OpClose (%v)", err)
}
s.dropConn(ctx, connection)
break
}
header.Masked = false
if err = s.processMessage(ctx, connection, header, payload); err != nil {
log.Print(err)
}
}
}
// On adding callback for message.
func (s *Server) On(name string, f HandlerFunc) {
s.mu.Lock()
s.callbacks[name] = f
s.mu.Unlock()
}
// NewChannel create new channel and proxy channel delConn
// for handling connection closing.
func (s *Server) NewChannel(id string) *Channel {
c := newChannel(id)
s.mu.Lock()
s.channels[id] = c
s.delChan = append(s.delChan, c.delConn)
s.mu.Unlock()
return c
}
// Channel find and return the channel.
func (s *Server) Channel(id string) *Channel {
s.mu.Lock()
defer s.mu.Unlock()
return s.channels[id]
}
// Channels returns channels id with live connections.
func (s *Server) Channels() []string {
s.mu.RLock()
defer s.mu.RUnlock()
list := []string{}
for _, c := range s.channels {
if c.Count() != 0 {
list = append(list, c.id)
}
}
return list
}
// OnConnect function which will be called when new connections come.
func (s *Server) OnConnect(f func(ctx context.Context, c *Conn)) {
s.mu.Lock()
s.onConnect = f
s.mu.Unlock()
}
// OnDisconnect function which will be called when new connections come.
func (s *Server) OnDisconnect(f func(ctx context.Context, c *Conn)) {
s.mu.Lock()
s.onDisconnect = f
s.mu.Unlock()
}
// OnMessage handling byte message. This function works as echo by default
func (s *Server) OnMessage(f func(ctx context.Context, c *Conn, h ws.Header, b []byte)) {
s.mu.Lock()
s.onMessage = f
s.mu.Unlock()
}
// Emit message to all connections.
func (s *Server) Emit(name string, data []byte) {
s.broadcast <- Message{
Name: name,
Data: data,
}
}
// SendTo send message to channel with id.
func (s *Server) SendTo(id string, name string, message *Message) error {
s.mu.Lock()
defer s.mu.Unlock()
ch := s.Channel(id)
if ch != nil {
ch.Emit(name, message)
}
return errors.New("no channel found")
}
// Count return number of active connections.
func (s *Server) Count() int {
s.mu.RLock()
defer s.mu.RUnlock()
return len(s.connections)
}
// IsClosed return the state of websocket server.
func (s *Server) IsClosed() bool {
s.mu.RLock()
defer s.mu.RUnlock()
return s.done
}
func (s *Server) processMessage(ctx context.Context, c *Conn, h ws.Header, b []byte) error {
if len(b) == 0 {
s.onMessage(ctx, c, h, b)
return nil
}
if h.OpCode != ws.OpBinary && h.OpCode != ws.OpText {
s.onMessage(ctx, c, h, b)
return nil
}
var msg struct {
Name string `json:"name"`
Data any `json:"data"`
}
if err := json.Unmarshal(b, &msg); err == nil && s.callbacks[msg.Name] != nil {
buf, err := json.Marshal(msg.Data)
if err != nil {
return err
}
s.callbacks[msg.Name](ctx, c, &Message{
Name: msg.Name,
Data: buf,
})
return nil
}
s.onMessage(ctx, c, h, b)
return nil
}
func (s *Server) addConn(ctx context.Context, conn *Conn) {
if s.onConnect != nil {
go s.onConnect(ctx, conn)
}
s.mu.Lock()
s.connections[conn] = true
s.mu.Unlock()
}
func (s *Server) dropConn(ctx context.Context, conn *Conn) {
if !reflect.ValueOf(s.onDisconnect).IsNil() {
go s.onDisconnect(ctx, conn)
}
go func() {
for _, dC := range s.delChan {
dC <- conn
}
}()
s.mu.Lock()
delete(s.connections, conn)
s.mu.Unlock()
}
func uuid() string {
b := make([]byte, 16)
_, _ = rand.Read(b)
return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
}