-
Notifications
You must be signed in to change notification settings - Fork 41
/
hub.go
81 lines (68 loc) · 1.76 KB
/
hub.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
package signalr
import (
"context"
"sync"
)
// HubInterface is a hubs interface
type HubInterface interface {
Initialize(hubContext HubContext)
OnConnected(connectionID string)
OnDisconnected(connectionID string)
}
// Hub is a base class for hubs
type Hub struct {
context HubContext
cm sync.RWMutex
}
// Initialize initializes a hub with a HubContext
func (h *Hub) Initialize(ctx HubContext) {
h.cm.Lock()
defer h.cm.Unlock()
h.context = ctx
}
// Clients returns the clients of this hub
func (h *Hub) Clients() HubClients {
h.cm.RLock()
defer h.cm.RUnlock()
return h.context.Clients()
}
// Groups returns the client groups of this hub
func (h *Hub) Groups() GroupManager {
h.cm.RLock()
defer h.cm.RUnlock()
return h.context.Groups()
}
// Items returns the items for this connection
func (h *Hub) Items() *sync.Map {
h.cm.RLock()
defer h.cm.RUnlock()
return h.context.Items()
}
// ConnectionID gets the ID of the current connection
func (h *Hub) ConnectionID() string {
h.cm.RLock()
defer h.cm.RUnlock()
return h.context.ConnectionID()
}
// Context is the context.Context of the current connection
func (h *Hub) Context() context.Context {
h.cm.RLock()
defer h.cm.RUnlock()
return h.context.Context()
}
// Abort aborts the current connection
func (h *Hub) Abort() {
h.cm.RLock()
defer h.cm.RUnlock()
h.context.Abort()
}
// Logger returns the loggers used in this server. By this, derived hubs can use the same loggers as the server.
func (h *Hub) Logger() (info StructuredLogger, dbg StructuredLogger) {
h.cm.RLock()
defer h.cm.RUnlock()
return h.context.Logger()
}
// OnConnected is called when the hub is connected
func (h *Hub) OnConnected(string) {}
// OnDisconnected is called when the hub is disconnected
func (h *Hub) OnDisconnected(string) {}