github.com/philippseith/signalr@v0.6.3/hub.go (about) 1 package signalr 2 3 import ( 4 "context" 5 "sync" 6 ) 7 8 // HubInterface is a hubs interface 9 type HubInterface interface { 10 Initialize(hubContext HubContext) 11 OnConnected(connectionID string) 12 OnDisconnected(connectionID string) 13 } 14 15 // Hub is a base class for hubs 16 type Hub struct { 17 context HubContext 18 cm sync.RWMutex 19 } 20 21 // Initialize initializes a hub with a HubContext 22 func (h *Hub) Initialize(ctx HubContext) { 23 h.cm.Lock() 24 defer h.cm.Unlock() 25 h.context = ctx 26 } 27 28 // Clients returns the clients of this hub 29 func (h *Hub) Clients() HubClients { 30 h.cm.RLock() 31 defer h.cm.RUnlock() 32 return h.context.Clients() 33 } 34 35 // Groups returns the client groups of this hub 36 func (h *Hub) Groups() GroupManager { 37 h.cm.RLock() 38 defer h.cm.RUnlock() 39 return h.context.Groups() 40 } 41 42 // Items returns the items for this connection 43 func (h *Hub) Items() *sync.Map { 44 h.cm.RLock() 45 defer h.cm.RUnlock() 46 return h.context.Items() 47 } 48 49 // ConnectionID gets the ID of the current connection 50 func (h *Hub) ConnectionID() string { 51 h.cm.RLock() 52 defer h.cm.RUnlock() 53 return h.context.ConnectionID() 54 } 55 56 // Context is the context.Context of the current connection 57 func (h *Hub) Context() context.Context { 58 h.cm.RLock() 59 defer h.cm.RUnlock() 60 return h.context.Context() 61 } 62 63 // Abort aborts the current connection 64 func (h *Hub) Abort() { 65 h.cm.RLock() 66 defer h.cm.RUnlock() 67 h.context.Abort() 68 } 69 70 // Logger returns the loggers used in this server. By this, derived hubs can use the same loggers as the server. 71 func (h *Hub) Logger() (info StructuredLogger, dbg StructuredLogger) { 72 h.cm.RLock() 73 defer h.cm.RUnlock() 74 return h.context.Logger() 75 } 76 77 // OnConnected is called when the hub is connected 78 func (h *Hub) OnConnected(string) {} 79 80 // OnDisconnected is called when the hub is disconnected 81 func (h *Hub) OnDisconnected(string) {}