-
-
Notifications
You must be signed in to change notification settings - Fork 308
/
websockets.go
61 lines (49 loc) · 1.18 KB
/
websockets.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 server
import (
"context"
"github.com/google/uuid"
"sync"
)
type WebsocketBag struct {
mu sync.Mutex
conns map[uuid.UUID]*context.CancelFunc
}
// Returns the websocket bag which contains all of the currently open websocket connections
// for the server instance.
func (s *Server) Websockets() *WebsocketBag {
s.wsBagLocker.Lock()
defer s.wsBagLocker.Unlock()
if s.wsBag == nil {
s.wsBag = &WebsocketBag{}
}
return s.wsBag
}
// Adds a new websocket connection to the stack.
func (w *WebsocketBag) Push(u uuid.UUID, cancel *context.CancelFunc) {
w.mu.Lock()
defer w.mu.Unlock()
if w.conns == nil {
w.conns = make(map[uuid.UUID]*context.CancelFunc)
}
w.conns[u] = cancel
}
// Removes a connection from the stack.
func (w *WebsocketBag) Remove(u uuid.UUID) {
w.mu.Lock()
delete(w.conns, u)
w.mu.Unlock()
}
// Cancels all of the stored cancel functions which has the effect of disconnecting
// every listening websocket for the server.
func (w *WebsocketBag) CancelAll() {
w.mu.Lock()
w.mu.Unlock()
if w.conns != nil {
for _, cancel := range w.conns {
c := *cancel
c()
}
}
// Reset the connections.
w.conns = make(map[uuid.UUID]*context.CancelFunc)
}