104 lines
3.6 KiB
C#
104 lines
3.6 KiB
C#
using System;
|
|
using System.Text.Json;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Logging;
|
|
using Server.Networking.Messages;
|
|
|
|
namespace GameServer.Dungeon;
|
|
|
|
public sealed partial class DungeonHandler
|
|
{
|
|
private async Task SubscribeToBroadcastChannelAsync()
|
|
{
|
|
try
|
|
{
|
|
// Subscribe to server:broadcast channel
|
|
await _redis.Bus.SubscribeAsync("server:broadcast", (channel, message) =>
|
|
{
|
|
try
|
|
{
|
|
if (message.IsNullOrEmpty) return;
|
|
|
|
var payload = JsonSerializer.Deserialize<BroadcastPayload>(message.ToString());
|
|
if (payload == null) return;
|
|
|
|
_log.LogInformation("Received broadcast from Redis: [{Type}] {Message}", payload.Type, payload.Message);
|
|
|
|
// Send to all local player sessions
|
|
foreach (var session in _playerSessions.Values)
|
|
{
|
|
_ = SendAsync(session.Connection, DungeonOpCodes.SERVER_BROADCAST, new
|
|
{
|
|
message = payload.Message,
|
|
type = payload.Type
|
|
}, CancellationToken.None);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_log.LogError(ex, "Error processing broadcast message");
|
|
}
|
|
});
|
|
|
|
// Subscribe to server:command channel for asset refresh, force reconnect, etc.
|
|
await _redis.Bus.SubscribeAsync("server:command", (channel, message) =>
|
|
{
|
|
try
|
|
{
|
|
if (message.IsNullOrEmpty) return;
|
|
|
|
var payload = JsonSerializer.Deserialize<CommandPayload>(message.ToString());
|
|
if (payload == null) return;
|
|
|
|
_log.LogInformation("Received command from Redis: {Command}", payload.Command);
|
|
|
|
switch (payload.Command)
|
|
{
|
|
case "refresh_assets":
|
|
// Queue asset refresh for all local players
|
|
_broadcastService.QueueAssetRefresh();
|
|
break;
|
|
case "force_reconnect":
|
|
// Could implement force disconnect here
|
|
break;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_log.LogError(ex, "Error processing command message");
|
|
}
|
|
});
|
|
|
|
_log.LogInformation("Subscribed to Redis broadcast channels");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_log.LogError(ex, "Failed to subscribe to Redis broadcast channels");
|
|
}
|
|
}
|
|
|
|
// Payload classes for Redis pub/sub
|
|
private class BroadcastPayload
|
|
{
|
|
[System.Text.Json.Serialization.JsonPropertyName("type")]
|
|
public string Type { get; set; } = "system";
|
|
|
|
[System.Text.Json.Serialization.JsonPropertyName("message")]
|
|
public string Message { get; set; } = "";
|
|
|
|
[System.Text.Json.Serialization.JsonPropertyName("duration")]
|
|
public int Duration { get; set; } = 10;
|
|
|
|
[System.Text.Json.Serialization.JsonPropertyName("timestamp")]
|
|
public long Timestamp { get; set; }
|
|
}
|
|
|
|
private class CommandPayload
|
|
{
|
|
[System.Text.Json.Serialization.JsonPropertyName("command")]
|
|
public string Command { get; set; } = "";
|
|
public long Timestamp { get; set; }
|
|
}
|
|
}
|