| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Sends a Server-Sent Event (SSE) to connected clients. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This function writes a Server-Sent Event (SSE) to the current HTTP response stream (per-connection). |
| | | 6 | | For server-wide broadcasting to all connected clients, use Send-KrSseBroadcastEvent. |
| | | 7 | | .PARAMETER Event |
| | | 8 | | The name of the event. |
| | | 9 | | .PARAMETER Data |
| | | 10 | | The data payload of the event. |
| | | 11 | | .PARAMETER id |
| | | 12 | | Optional: The event ID. |
| | | 13 | | .PARAMETER retryMs |
| | | 14 | | Optional: The retry interval in milliseconds. |
| | | 15 | | .EXAMPLE |
| | | 16 | | Write-KrSseEvent -Event 'message' -Data 'Hello, SSE!' |
| | | 17 | | Sends an SSE with event name 'message' and data 'Hello, SSE!'. |
| | | 18 | | .EXAMPLE |
| | | 19 | | Write-KrSseEvent -Event 'update' -Data '{"status":"ok"}' -id '123' -retryMs 5000 |
| | | 20 | | Sends an SSE with event name 'update', JSON data, event ID '123', and a retry interval of 5000 milliseconds |
| | | 21 | | .NOTES |
| | | 22 | | Use Start-KrSseResponse before sending events. |
| | | 23 | | #> |
| | | 24 | | function Write-KrSseEvent { |
| | | 25 | | [KestrunRuntimeApi('Route')] |
| | | 26 | | [CmdletBinding()] |
| | | 27 | | param( |
| | | 28 | | [Parameter(Mandatory = $true)] |
| | | 29 | | [string] $Event, |
| | | 30 | | [Parameter(Mandatory = $true)] |
| | | 31 | | [string] $Data, |
| | | 32 | | [Parameter(Mandatory = $false)] |
| | | 33 | | [string] $id, |
| | | 34 | | [Parameter(Mandatory = $false)] |
| | | 35 | | [int] $retryMs |
| | | 36 | | ) |
| | | 37 | | |
| | | 38 | | # Only works inside a route script block where $Context is available |
| | 0 | 39 | | if ($null -ne $Context) { |
| | 0 | 40 | | $Context.WriteSseEvent( $Event, $Data, $id, $retryMs) |
| | 0 | 41 | | Write-KrLog -Level Debug -Message "Sse event sent: $Event - $Data" |
| | | 42 | | return |
| | | 43 | | } |
| | | 44 | | |
| | 0 | 45 | | Write-KrOutsideRouteWarning |
| | | 46 | | } |