增加用户管理

This commit is contained in:
wtz
2026-02-20 23:39:49 +08:00
parent a272dad5f1
commit af3b805dbf
18 changed files with 1493 additions and 53 deletions

View File

@@ -69,7 +69,7 @@ func (h *Hub) Run() {
}
h.mu.Unlock()
if c.RoomID != "" {
h.GameMgr.LeaveRoomWithTTL(c.RoomID, c.ID)
h.GameMgr.MarkPlayerOffline(c.RoomID, c.ID)
h.broadcastRoomState(c.RoomID)
}
@@ -201,7 +201,20 @@ func (c *Client) handleMessage(data []byte) {
c.Hub.broadcastRoomState(c.RoomID)
case models.MsgTypeChat:
c.Hub.broadcastToRoom(c.RoomID, msg)
chatMsg, ok := msg.Data.(string)
if !ok || chatMsg == "" {
return
}
playerName := c.Hub.GameMgr.GetPlayerName(c.RoomID, c.ID)
c.Hub.broadcastToRoom(c.RoomID, models.Message{
Type: models.MsgTypeChat,
Data: map[string]string{
"playerId": c.ID,
"playerName": playerName,
"message": chatMsg,
},
Timestamp: time.Now().Unix(),
})
}
}
@@ -300,6 +313,33 @@ func (h *Hub) broadcastRoomState(roomID string) {
}
}
func (h *Hub) BroadcastRoomLeft(roomID, playerID string) {
h.mu.RLock()
clients := make([]*Client, 0)
for _, c := range h.Clients {
if c.RoomID == roomID {
clients = append(clients, c)
}
}
h.mu.RUnlock()
data, _ := json.Marshal(models.Message{
Type: models.MsgTypeLeave,
PlayerID: playerID,
Timestamp: time.Now().Unix(),
})
for _, c := range clients {
select {
case c.Send <- data:
case <-c.done:
default:
}
}
h.broadcastRoomState(roomID)
}
func ServeWs(hub *Hub, w http.ResponseWriter, r *http.Request) {
roomID := strings.ToLower(r.URL.Query().Get("roomId"))
playerID := r.URL.Query().Get("playerId")