Improve Redis key handling and spectator sync logic

Enhances Redis key type checks and cleanup in message system, adds periodic cleanup task, and improves error handling for Redis operations. Refines multiplayer and spectator hub logic to better synchronize player states and prevent invalid spectator sessions. Adds more detailed logging for channel/user join/leave events and spectator watch requests.
This commit is contained in:
咕谷酱
2025-08-22 23:53:53 +08:00
committed by MingxuanGame
parent d4f542c64b
commit 5959254de6
5 changed files with 166 additions and 30 deletions

View File

@@ -92,6 +92,19 @@ async def send_message(
channel_name = db_channel.name
user_id = current_user.id
# 对于多人游戏房间在发送消息前进行Redis键检查
if channel_type == ChannelType.MULTIPLAYER:
try:
from app.dependencies.database import get_redis
redis = get_redis()
key = f"channel:{channel_id}:messages"
key_type = await redis.type(key)
if key_type not in ["none", "zset"]:
logger.warning(f"Fixing Redis key {key} with wrong type: {key_type}")
await redis.delete(key)
except Exception as e:
logger.warning(f"Failed to check/fix Redis key for channel {channel_id}: {e}")
# 使用 Redis 消息系统发送消息 - 立即返回
resp = await redis_message_system.send_message(
channel_id=channel_id,