| | | 1 | | using Kestrun.SignalR; |
| | | 2 | | |
| | | 3 | | namespace Kestrun.Hosting; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Extension methods for KestrunHost to support SignalR real-time broadcasting. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class KestrunHostSignalRExtensions |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Gets the number of currently connected SignalR clients if the Kestrun hub is configured. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="host">The KestrunHost instance.</param> |
| | | 14 | | /// <returns>The count of connected clients, or null if SignalR hub/connection tracking is not configured.</returns> |
| | | 15 | | public static int? GetConnectedClientCount(this KestrunHost host) |
| | | 16 | | { |
| | 0 | 17 | | var svcProvider = host.App?.Services; |
| | 0 | 18 | | return svcProvider == null |
| | 0 | 19 | | ? null |
| | 0 | 20 | | : svcProvider.GetService(typeof(IConnectionTracker)) is IConnectionTracker tracker ? tracker.ConnectedCount |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Broadcasts a log message to all connected SignalR clients using the best available service provider. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="host">The KestrunHost instance.</param> |
| | | 27 | | /// <param name="level">The log level (e.g., Information, Warning, Error, Debug, Verbose).</param> |
| | | 28 | | /// <param name="message">The log message to broadcast.</param> |
| | | 29 | | /// <param name="httpContext">Optional: The current HttpContext, if available.</param> |
| | | 30 | | /// <param name="cancellationToken">Optional: Cancellation token.</param> |
| | | 31 | | public static async Task<bool> BroadcastLogAsync(this KestrunHost host, string level, string message, HttpContext? h |
| | | 32 | | { |
| | 0 | 33 | | if (httpContext != null) |
| | | 34 | | { |
| | 0 | 35 | | var ctx = new Models.KestrunContext(host, httpContext); |
| | 0 | 36 | | return await ctx.BroadcastLogAsync(level, message, cancellationToken).ConfigureAwait(false); |
| | | 37 | | } |
| | | 38 | | // Fallback to service resolution when no HttpContext |
| | 0 | 39 | | var svcProvider = host.App?.Services; |
| | 0 | 40 | | if (svcProvider == null) |
| | | 41 | | { |
| | 0 | 42 | | host.Logger.Warning("No service provider available to resolve IRealtimeBroadcaster."); |
| | 0 | 43 | | return false; |
| | | 44 | | } |
| | 0 | 45 | | if (svcProvider.GetService(typeof(IRealtimeBroadcaster)) is not IRealtimeBroadcaster broadcaster) |
| | | 46 | | { |
| | 0 | 47 | | host.Logger.Warning("IRealtimeBroadcaster service is not registered. Make sure SignalR is configured with Ke |
| | 0 | 48 | | return false; |
| | | 49 | | } |
| | | 50 | | try |
| | | 51 | | { |
| | 0 | 52 | | await broadcaster.BroadcastLogAsync(level, message, cancellationToken).ConfigureAwait(false); |
| | 0 | 53 | | host.Logger.Debug("Broadcasted log message via SignalR: {Level} - {Message}", level, message); |
| | 0 | 54 | | return true; |
| | | 55 | | } |
| | 0 | 56 | | catch (Exception ex) |
| | | 57 | | { |
| | 0 | 58 | | host.Logger.Error(ex, "Failed to broadcast log message: {Level} - {Message}", level, message); |
| | 0 | 59 | | return false; |
| | | 60 | | } |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Broadcasts an event to all connected SignalR clients using the best available service provider. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="host">The KestrunHost instance.</param> |
| | | 67 | | /// <param name="eventName">The name of the event to broadcast.</param> |
| | | 68 | | /// <param name="data">Optional data to include with the event.</param> |
| | | 69 | | /// <param name="httpContext">Optional: The current HttpContext, if available.</param> |
| | | 70 | | /// <param name="cancellationToken">Optional: Cancellation token.</param> |
| | | 71 | | public static async Task<bool> BroadcastEventAsync(this KestrunHost host, string eventName, object? data = null, Htt |
| | | 72 | | { |
| | 0 | 73 | | if (httpContext != null) |
| | | 74 | | { |
| | 0 | 75 | | var ctx = new Models.KestrunContext(host, httpContext); |
| | 0 | 76 | | return await ctx.BroadcastEventAsync(eventName, data, cancellationToken).ConfigureAwait(false); |
| | | 77 | | } |
| | 0 | 78 | | var svcProvider = host.App?.Services; |
| | 0 | 79 | | if (svcProvider == null) |
| | | 80 | | { |
| | 0 | 81 | | host.Logger.Warning("No service provider available to resolve IRealtimeBroadcaster."); |
| | 0 | 82 | | return false; |
| | | 83 | | } |
| | 0 | 84 | | if (svcProvider.GetService(typeof(IRealtimeBroadcaster)) is not IRealtimeBroadcaster broadcaster) |
| | | 85 | | { |
| | 0 | 86 | | host.Logger.Warning("IRealtimeBroadcaster service is not registered. Make sure SignalR is configured with Ke |
| | 0 | 87 | | return false; |
| | | 88 | | } |
| | | 89 | | try |
| | | 90 | | { |
| | 0 | 91 | | await broadcaster.BroadcastEventAsync(eventName, data, cancellationToken).ConfigureAwait(false); |
| | 0 | 92 | | host.Logger.Debug("Broadcasted event via SignalR: {EventName}", eventName); |
| | 0 | 93 | | return true; |
| | | 94 | | } |
| | 0 | 95 | | catch (Exception ex) |
| | | 96 | | { |
| | 0 | 97 | | host.Logger.Error(ex, "Failed to broadcast event: {EventName}", eventName); |
| | 0 | 98 | | return false; |
| | | 99 | | } |
| | 0 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Broadcasts a message to a specific SignalR group using the best available service provider. |
| | | 104 | | /// </summary> |
| | | 105 | | /// <param name="host">The KestrunHost instance.</param> |
| | | 106 | | /// <param name="groupName">The name of the group to broadcast to.</param> |
| | | 107 | | /// <param name="method">The hub method name to invoke on clients.</param> |
| | | 108 | | /// <param name="message">The message to broadcast.</param> |
| | | 109 | | /// <param name="httpContext">Optional: The current HttpContext, if available.</param> |
| | | 110 | | /// <param name="cancellationToken">Optional: Cancellation token.</param> |
| | | 111 | | public static async Task<bool> BroadcastToGroupAsync(this KestrunHost host, string groupName, string method, object? |
| | | 112 | | { |
| | 0 | 113 | | if (httpContext != null) |
| | | 114 | | { |
| | 0 | 115 | | var ctx = new Models.KestrunContext(host, httpContext); |
| | 0 | 116 | | return await ctx.BroadcastToGroupAsync(groupName, method, message, cancellationToken).ConfigureAwait(false); |
| | | 117 | | } |
| | 0 | 118 | | var svcProvider = host.App?.Services; |
| | 0 | 119 | | if (svcProvider == null) |
| | | 120 | | { |
| | 0 | 121 | | host.Logger.Warning("No service provider available to resolve IRealtimeBroadcaster."); |
| | 0 | 122 | | return false; |
| | | 123 | | } |
| | 0 | 124 | | if (svcProvider.GetService(typeof(IRealtimeBroadcaster)) is not IRealtimeBroadcaster broadcaster) |
| | | 125 | | { |
| | 0 | 126 | | host.Logger.Warning("IRealtimeBroadcaster service is not registered. Make sure SignalR is configured with Ke |
| | 0 | 127 | | return false; |
| | | 128 | | } |
| | | 129 | | try |
| | | 130 | | { |
| | 0 | 131 | | await broadcaster.BroadcastToGroupAsync(groupName, method, message, cancellationToken).ConfigureAwait(false) |
| | 0 | 132 | | host.Logger.Debug("Broadcasted to group {GroupName} via method {Method}", groupName, method); |
| | 0 | 133 | | return true; |
| | | 134 | | } |
| | 0 | 135 | | catch (Exception ex) |
| | | 136 | | { |
| | 0 | 137 | | host.Logger.Error(ex, "Failed to broadcast to group {GroupName}", groupName); |
| | 0 | 138 | | return false; |
| | | 139 | | } |
| | 0 | 140 | | } |
| | | 141 | | } |