| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Broadcasts a message to a specific SignalR group. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This function sends a message to all clients in a specific SignalR group via the IRealtimeBroadcaster service. |
| | | 6 | | The message is broadcast in real-time to all connected clients listening in the specified group. |
| | | 7 | | .PARAMETER GroupName |
| | | 8 | | The name of the group to broadcast to. |
| | | 9 | | .PARAMETER Method |
| | | 10 | | The hub method name to invoke on clients. |
| | | 11 | | .PARAMETER Message |
| | | 12 | | The message to broadcast to the group. |
| | | 13 | | .PARAMETER Server |
| | | 14 | | The Kestrun server instance. If not specified, the default server is used. |
| | | 15 | | .EXAMPLE |
| | | 16 | | Send-KrSignalRGroupMessage -GroupName "Admins" -Method "ReceiveAdminUpdate" -Message @{ Update = "System mainten |
| | | 17 | | Broadcasts an admin update to all clients in the "Admins" group. |
| | | 18 | | .EXAMPLE |
| | | 19 | | Send-KrSignalRGroupMessage -GroupName "Workers" -Method "ReceiveTaskUpdate" -Message @{ TaskId = 123; Progress = |
| | | 20 | | Broadcasts a task progress update to all clients in the "Workers" group. |
| | | 21 | | .NOTES |
| | | 22 | | This function requires that SignalR has been configured on the server using Add-KrSignalRHubMiddleware. |
| | | 23 | | The IRealtimeBroadcaster service must be registered for this cmdlet to work. |
| | | 24 | | #> |
| | | 25 | | function Send-KrSignalRGroupMessage { |
| | | 26 | | [KestrunRuntimeApi('Everywhere')] |
| | | 27 | | [CmdletBinding()] |
| | | 28 | | param( |
| | | 29 | | [Parameter(Mandatory = $true)] |
| | | 30 | | [string]$GroupName, |
| | | 31 | | |
| | | 32 | | [Parameter(Mandatory = $true)] |
| | | 33 | | [string]$Method, |
| | | 34 | | |
| | | 35 | | [Parameter(Mandatory = $true)] |
| | | 36 | | [object]$Message, |
| | | 37 | | |
| | | 38 | | [Parameter(ValueFromPipeline)] |
| | | 39 | | [Kestrun.Hosting.KestrunHost]$Server |
| | | 40 | | ) |
| | | 41 | | |
| | | 42 | | process { |
| | | 43 | | try { |
| | 0 | 44 | | if ($null -ne $Context) { |
| | 0 | 45 | | if ($Context.BroadcastToGroup($GroupName, $Method, $Message, [System.Threading.CancellationToken]::None) |
| | 0 | 46 | | Write-KrLog -Level Debug -Message "Broadcasted to group '$GroupName' via method '$Method': $Message" |
| | | 47 | | return |
| | | 48 | | } else { |
| | 0 | 49 | | Write-KrLog -Level Error -Message "Failed to broadcast to group '$GroupName': Unknown error" |
| | | 50 | | return |
| | | 51 | | } |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | # Fallback: allow explicit host usage outside a route (no HttpContext) |
| | 0 | 55 | | if (-not $Server) { |
| | 0 | 56 | | try { $Server = Get-KrServer } catch { $Server = $null } |
| | | 57 | | } |
| | 0 | 58 | | if ($null -ne $Server) { |
| | 0 | 59 | | $task = [Kestrun.Hosting.KestrunHostSignalRExtensions]::BroadcastToGroupAsync($Server, $GroupName, $Meth |
| | 0 | 60 | | $null = $task.GetAwaiter().GetResult() |
| | 0 | 61 | | Write-KrLog -Level Debug -Message "Broadcasted to group '$GroupName' via method '$Method' (host fallback |
| | | 62 | | } else { |
| | 0 | 63 | | Write-KrOutsideRouteWarning |
| | | 64 | | } |
| | | 65 | | } catch { |
| | 0 | 66 | | Write-KrLog -Level Error -Message "Failed to broadcast to group '$GroupName': $_" -Exception $_.Exception |
| | | 67 | | } |
| | | 68 | | } |
| | | 69 | | } |