| | | 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 | | { |
| | | 17 | | try |
| | | 18 | | { |
| | 4 | 19 | | var svcProvider = host.App?.Services; |
| | 3 | 20 | | return svcProvider == null |
| | 3 | 21 | | ? null |
| | 3 | 22 | | : svcProvider.GetService(typeof(IConnectionTracker)) is IConnectionTracker tracker ? tracker.ConnectedCo |
| | | 23 | | } |
| | 1 | 24 | | catch (InvalidOperationException) |
| | | 25 | | { |
| | | 26 | | // App not built yet |
| | 1 | 27 | | return null; |
| | | 28 | | } |
| | 4 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Broadcasts a log message to all connected SignalR clients using the best available service provider. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="host">The KestrunHost instance.</param> |
| | | 35 | | /// <param name="level">The log level (e.g., Information, Warning, Error, Debug, Verbose).</param> |
| | | 36 | | /// <param name="message">The log message to broadcast.</param> |
| | | 37 | | /// <param name="httpContext">Optional: The current HttpContext, if available.</param> |
| | | 38 | | /// <param name="cancellationToken">Optional: Cancellation token.</param> |
| | | 39 | | public static async Task<bool> BroadcastLogAsync(this KestrunHost host, string level, string message, HttpContext? h |
| | | 40 | | { |
| | 8 | 41 | | if (httpContext != null) |
| | | 42 | | { |
| | 0 | 43 | | var ctx = new Models.KestrunContext(host, httpContext); |
| | 0 | 44 | | return await ctx.BroadcastLogAsync(level, message, cancellationToken).ConfigureAwait(false); |
| | | 45 | | } |
| | | 46 | | // Fallback to service resolution when no HttpContext |
| | | 47 | | try |
| | | 48 | | { |
| | 8 | 49 | | var svcProvider = host.App?.Services; |
| | 7 | 50 | | if (svcProvider == null) |
| | | 51 | | { |
| | 0 | 52 | | host.Logger.Warning("No service provider available to resolve IRealtimeBroadcaster."); |
| | 0 | 53 | | return false; |
| | | 54 | | } |
| | 7 | 55 | | if (svcProvider.GetService(typeof(IRealtimeBroadcaster)) is not IRealtimeBroadcaster broadcaster) |
| | | 56 | | { |
| | 1 | 57 | | host.Logger.Warning("IRealtimeBroadcaster service is not registered. Make sure SignalR is configured wit |
| | 1 | 58 | | return false; |
| | | 59 | | } |
| | | 60 | | try |
| | | 61 | | { |
| | 6 | 62 | | await broadcaster.BroadcastLogAsync(level, message, cancellationToken).ConfigureAwait(false); |
| | 5 | 63 | | host.Logger.Debug("Broadcasted log message via SignalR: {Level} - {Message}", level, message); |
| | 5 | 64 | | return true; |
| | | 65 | | } |
| | 1 | 66 | | catch (Exception ex) |
| | | 67 | | { |
| | 1 | 68 | | host.Logger.Error(ex, "Failed to broadcast log message: {Level} - {Message}", level, message); |
| | 1 | 69 | | return false; |
| | | 70 | | } |
| | | 71 | | } |
| | 1 | 72 | | catch (InvalidOperationException) |
| | | 73 | | { |
| | | 74 | | // App not built yet |
| | 1 | 75 | | host.Logger.Warning("WebApplication is not built yet. Call Build() first."); |
| | 1 | 76 | | return false; |
| | | 77 | | } |
| | 8 | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Broadcasts an event to all connected SignalR clients using the best available service provider. |
| | | 82 | | /// </summary> |
| | | 83 | | /// <param name="host">The KestrunHost instance.</param> |
| | | 84 | | /// <param name="eventName">The name of the event to broadcast.</param> |
| | | 85 | | /// <param name="data">Optional data to include with the event.</param> |
| | | 86 | | /// <param name="httpContext">Optional: The current HttpContext, if available.</param> |
| | | 87 | | /// <param name="cancellationToken">Optional: Cancellation token.</param> |
| | | 88 | | public static async Task<bool> BroadcastEventAsync(this KestrunHost host, string eventName, object? data = null, Htt |
| | | 89 | | { |
| | 5 | 90 | | if (httpContext != null) |
| | | 91 | | { |
| | 0 | 92 | | var ctx = new Models.KestrunContext(host, httpContext); |
| | 0 | 93 | | return await ctx.BroadcastEventAsync(eventName, data, cancellationToken).ConfigureAwait(false); |
| | | 94 | | } |
| | | 95 | | try |
| | | 96 | | { |
| | 5 | 97 | | var svcProvider = host.App?.Services; |
| | 4 | 98 | | if (svcProvider == null) |
| | | 99 | | { |
| | 0 | 100 | | host.Logger.Warning("No service provider available to resolve IRealtimeBroadcaster."); |
| | 0 | 101 | | return false; |
| | | 102 | | } |
| | 4 | 103 | | if (svcProvider.GetService(typeof(IRealtimeBroadcaster)) is not IRealtimeBroadcaster broadcaster) |
| | | 104 | | { |
| | 1 | 105 | | host.Logger.Warning("IRealtimeBroadcaster service is not registered. Make sure SignalR is configured wit |
| | 1 | 106 | | return false; |
| | | 107 | | } |
| | | 108 | | try |
| | | 109 | | { |
| | 3 | 110 | | await broadcaster.BroadcastEventAsync(eventName, data, cancellationToken).ConfigureAwait(false); |
| | 2 | 111 | | host.Logger.Debug("Broadcasted event via SignalR: {EventName}", eventName); |
| | 2 | 112 | | return true; |
| | | 113 | | } |
| | 1 | 114 | | catch (Exception ex) |
| | | 115 | | { |
| | 1 | 116 | | host.Logger.Error(ex, "Failed to broadcast event: {EventName}", eventName); |
| | 1 | 117 | | return false; |
| | | 118 | | } |
| | | 119 | | } |
| | 1 | 120 | | catch (InvalidOperationException) |
| | | 121 | | { |
| | | 122 | | // App not built yet |
| | 1 | 123 | | host.Logger.Warning("WebApplication is not built yet. Call Build() first."); |
| | 1 | 124 | | return false; |
| | | 125 | | } |
| | 5 | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Broadcasts a message to a specific SignalR group using the best available service provider. |
| | | 130 | | /// </summary> |
| | | 131 | | /// <param name="host">The KestrunHost instance.</param> |
| | | 132 | | /// <param name="groupName">The name of the group to broadcast to.</param> |
| | | 133 | | /// <param name="method">The hub method name to invoke on clients.</param> |
| | | 134 | | /// <param name="message">The message to broadcast.</param> |
| | | 135 | | /// <param name="httpContext">Optional: The current HttpContext, if available.</param> |
| | | 136 | | /// <param name="cancellationToken">Optional: Cancellation token.</param> |
| | | 137 | | public static async Task<bool> BroadcastToGroupAsync(this KestrunHost host, string groupName, string method, object? |
| | | 138 | | { |
| | 8 | 139 | | if (httpContext != null) |
| | | 140 | | { |
| | 0 | 141 | | var ctx = new Models.KestrunContext(host, httpContext); |
| | 0 | 142 | | return await ctx.BroadcastToGroupAsync(groupName, method, message, cancellationToken).ConfigureAwait(false); |
| | | 143 | | } |
| | | 144 | | try |
| | | 145 | | { |
| | 8 | 146 | | var svcProvider = host.App?.Services; |
| | 7 | 147 | | if (svcProvider == null) |
| | | 148 | | { |
| | 0 | 149 | | host.Logger.Warning("No service provider available to resolve IRealtimeBroadcaster."); |
| | 0 | 150 | | return false; |
| | | 151 | | } |
| | 7 | 152 | | if (svcProvider.GetService(typeof(IRealtimeBroadcaster)) is not IRealtimeBroadcaster broadcaster) |
| | | 153 | | { |
| | 1 | 154 | | host.Logger.Warning("IRealtimeBroadcaster service is not registered. Make sure SignalR is configured wit |
| | 1 | 155 | | return false; |
| | | 156 | | } |
| | | 157 | | try |
| | | 158 | | { |
| | 6 | 159 | | await broadcaster.BroadcastToGroupAsync(groupName, method, message, cancellationToken).ConfigureAwait(fa |
| | 5 | 160 | | host.Logger.Debug("Broadcasted to group {GroupName} via method {Method}", groupName, method); |
| | 5 | 161 | | return true; |
| | | 162 | | } |
| | 1 | 163 | | catch (Exception ex) |
| | | 164 | | { |
| | 1 | 165 | | host.Logger.Error(ex, "Failed to broadcast to group {GroupName}", groupName); |
| | 1 | 166 | | return false; |
| | | 167 | | } |
| | | 168 | | } |
| | 1 | 169 | | catch (InvalidOperationException) |
| | | 170 | | { |
| | | 171 | | // App not built yet |
| | 1 | 172 | | host.Logger.Warning("WebApplication is not built yet. Call Build() first."); |
| | 1 | 173 | | return false; |
| | | 174 | | } |
| | 8 | 175 | | } |
| | | 176 | | } |