| | | 1 | | using Microsoft.AspNetCore.SignalR; |
| | | 2 | | using Kestrun.Utilities.Json; |
| | | 3 | | |
| | | 4 | | namespace Kestrun.SignalR; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Default implementation of <see cref="IRealtimeBroadcaster"/> that broadcasts messages via SignalR. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// Initializes a new instance of the <see cref="RealtimeBroadcaster"/> class. |
| | | 11 | | /// </remarks> |
| | | 12 | | /// <param name="hubContext">The SignalR hub context for KestrunHub.</param> |
| | | 13 | | /// <param name="logger">The Serilog logger instance.</param> |
| | 0 | 14 | | public class RealtimeBroadcaster(IHubContext<KestrunHub> hubContext, Serilog.ILogger logger) : IRealtimeBroadcaster |
| | | 15 | | { |
| | 0 | 16 | | private readonly IHubContext<KestrunHub> _hubContext = hubContext; |
| | 0 | 17 | | private readonly Serilog.ILogger _logger = logger; |
| | | 18 | | |
| | | 19 | | /// <inheritdoc/> |
| | | 20 | | public async Task BroadcastLogAsync(string level, string message, CancellationToken cancellationToken = default) |
| | | 21 | | { |
| | | 22 | | try |
| | | 23 | | { |
| | 0 | 24 | | await _hubContext.Clients.All.SendAsync( |
| | 0 | 25 | | "ReceiveLog", |
| | 0 | 26 | | new |
| | 0 | 27 | | { |
| | 0 | 28 | | level, |
| | 0 | 29 | | message, |
| | 0 | 30 | | timestamp = DateTime.UtcNow |
| | 0 | 31 | | }, |
| | 0 | 32 | | cancellationToken); |
| | | 33 | | |
| | 0 | 34 | | _logger.Debug("Broadcasted log message: {Level} - {Message}", level, message); |
| | 0 | 35 | | } |
| | 0 | 36 | | catch (Exception ex) |
| | | 37 | | { |
| | 0 | 38 | | _logger.Error(ex, "Failed to broadcast log message: {Level} - {Message}", level, message); |
| | 0 | 39 | | } |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc/> |
| | | 43 | | public async Task BroadcastEventAsync(string eventName, object? data, CancellationToken cancellationToken = default) |
| | | 44 | | { |
| | | 45 | | try |
| | | 46 | | { |
| | | 47 | | // Sanitize data to avoid object cycles (PSObject graphs, etc.) |
| | 0 | 48 | | var safe = PayloadSanitizer.Sanitize(data); |
| | 0 | 49 | | await _hubContext.Clients.All.SendAsync( |
| | 0 | 50 | | "ReceiveEvent", |
| | 0 | 51 | | new |
| | 0 | 52 | | { |
| | 0 | 53 | | eventName, |
| | 0 | 54 | | data = safe, |
| | 0 | 55 | | timestamp = DateTime.UtcNow |
| | 0 | 56 | | }, |
| | 0 | 57 | | cancellationToken); |
| | | 58 | | |
| | 0 | 59 | | _logger.Debug("Broadcasted event: {EventName}", eventName); |
| | 0 | 60 | | } |
| | 0 | 61 | | catch (Exception ex) |
| | | 62 | | { |
| | 0 | 63 | | _logger.Error(ex, "Failed to broadcast event: {EventName}", eventName); |
| | 0 | 64 | | } |
| | 0 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <inheritdoc/> |
| | | 68 | | public async Task BroadcastToGroupAsync(string groupName, string method, object? message, CancellationToken cancella |
| | | 69 | | { |
| | | 70 | | try |
| | | 71 | | { |
| | 0 | 72 | | var safe = PayloadSanitizer.Sanitize(message); |
| | 0 | 73 | | await _hubContext.Clients.Group(groupName).SendAsync(method, safe, cancellationToken); |
| | 0 | 74 | | _logger.Debug("Broadcasted message to group {GroupName} via method {Method}", groupName, method); |
| | 0 | 75 | | } |
| | 0 | 76 | | catch (Exception ex) |
| | | 77 | | { |
| | 0 | 78 | | _logger.Error(ex, "Failed to broadcast to group {GroupName}", groupName); |
| | 0 | 79 | | } |
| | 0 | 80 | | } |
| | | 81 | | } |