监控网页实现
This commit is contained in:
@@ -40,6 +40,12 @@ type Hub struct {
|
||||
mutex sync.RWMutex
|
||||
}
|
||||
|
||||
type StatsSnapshot struct {
|
||||
RoomCount int `json:"room_count"`
|
||||
TotalConnectedClient int `json:"total_connected_client"`
|
||||
RoomClients map[string]int `json:"room_clients"`
|
||||
}
|
||||
|
||||
func NewHub() *Hub {
|
||||
return &Hub{
|
||||
broadcast: make(chan Message),
|
||||
@@ -195,3 +201,22 @@ func InitChat() {
|
||||
MainHub = NewHub()
|
||||
go MainHub.Run()
|
||||
}
|
||||
|
||||
func (h *Hub) GetStatsSnapshot() StatsSnapshot {
|
||||
h.mutex.RLock()
|
||||
defer h.mutex.RUnlock()
|
||||
|
||||
roomClients := make(map[string]int, len(h.rooms))
|
||||
totalClients := 0
|
||||
for roomID, clients := range h.rooms {
|
||||
count := len(clients)
|
||||
roomClients[roomID] = count
|
||||
totalClients += count
|
||||
}
|
||||
|
||||
return StatsSnapshot{
|
||||
RoomCount: len(h.rooms),
|
||||
TotalConnectedClient: totalClients,
|
||||
RoomClients: roomClients,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user