监控网页实现

This commit is contained in:
Z
2026-04-09 00:14:57 +08:00
parent 6b1c7242c7
commit 1cce5634b1
17 changed files with 1186 additions and 38 deletions

View File

@@ -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,
}
}