| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Broadcasts a custom event to all connected SignalR clients. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This function sends a custom event with optional data to all connected SignalR clients via the IRealtimeBroadcas |
| | | 6 | | The event is broadcast in real-time to all connected clients listening on the hub. |
| | | 7 | | .PARAMETER EventName |
| | | 8 | | The name of the event to broadcast. |
| | | 9 | | .PARAMETER Data |
| | | 10 | | Optional data to include with the event. Can be any object that will be serialized to JSON. |
| | | 11 | | .PARAMETER Server |
| | | 12 | | The Kestrun server instance. If not specified, the default server is used. |
| | | 13 | | .EXAMPLE |
| | | 14 | | Send-KrSignalREvent -EventName "UserLoggedIn" -Data @{ Username = "admin"; Timestamp = (Get-Date) } |
| | | 15 | | Broadcasts a custom event with data to all connected SignalR clients. |
| | | 16 | | .EXAMPLE |
| | | 17 | | Send-KrSignalREvent -EventName "ServerHealthCheck" -Data @{ Status = "Healthy"; Uptime = 3600 } |
| | | 18 | | Broadcasts a health check event with status information. |
| | | 19 | | .EXAMPLE |
| | | 20 | | Get-KrServer | Send-KrSignalREvent -EventName "TaskCompleted" -Data @{ TaskId = 123; Success = $true } |
| | | 21 | | Broadcasts a task completion event using the pipeline. |
| | | 22 | | .NOTES |
| | | 23 | | This function requires that SignalR has been configured on the server using Add-KrSignalRHubMiddleware. |
| | | 24 | | The IRealtimeBroadcaster service must be registered for this cmdlet to work. |
| | | 25 | | #> |
| | | 26 | | function Send-KrSignalREvent { |
| | | 27 | | [KestrunRuntimeApi('Everywhere')] |
| | | 28 | | [CmdletBinding()] |
| | | 29 | | param( |
| | | 30 | | [Parameter(Mandatory = $true)] |
| | | 31 | | [string]$EventName, |
| | | 32 | | |
| | | 33 | | [Parameter()] |
| | | 34 | | [object]$Data, |
| | | 35 | | |
| | | 36 | | [Parameter(ValueFromPipeline)] |
| | | 37 | | [Kestrun.Hosting.KestrunHost]$Server |
| | | 38 | | ) |
| | | 39 | | |
| | | 40 | | process { |
| | | 41 | | try { |
| | 0 | 42 | | if ($null -ne $Context) { |
| | | 43 | | # Prefer the centralized KestrunContext implementation when inside a route |
| | 0 | 44 | | if ($Context.BroadcastEvent($EventName, $Data, [System.Threading.CancellationToken]::None)) { |
| | 0 | 45 | | Write-KrLog -Level Debug -Message "Broadcasted event: $EventName - $Data" |
| | | 46 | | return |
| | | 47 | | } else { |
| | 0 | 48 | | Write-KrLog -Level Error -Message 'Failed to broadcast event: Unknown error' |
| | | 49 | | return |
| | | 50 | | } |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | # Fallback: allow explicit host usage outside a route (no HttpContext) |
| | 0 | 54 | | if (-not $Server) { |
| | 0 | 55 | | try { $Server = Get-KrServer } catch { $Server = $null } |
| | | 56 | | } |
| | 0 | 57 | | if ($null -ne $Server) { |
| | 0 | 58 | | $task = [Kestrun.Hosting.KestrunHostSignalRExtensions]::BroadcastEventAsync($Server, $EventName, $Data, |
| | 0 | 59 | | $null = $task.GetAwaiter().GetResult() |
| | 0 | 60 | | Write-KrLog -Level Debug -Message "Broadcasted event (host fallback): $EventName - $Data" |
| | | 61 | | } else { |
| | 0 | 62 | | Write-KrOutsideRouteWarning |
| | | 63 | | } |
| | | 64 | | } catch { |
| | 0 | 65 | | Write-KrLog -Level Error -Message "Failed to broadcast event: $_" -Exception $_.Exception |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | } |