| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Maps a SignalR hub class to the given URL path. |
| | 4 | | .DESCRIPTION |
| | 5 | | This function allows you to map a SignalR hub class to a specific URL path on the Kestrun server. |
| | 6 | | .PARAMETER Server |
| | 7 | | The Kestrun server instance to which the SignalR hub will be added. |
| | 8 | | .PARAMETER HubType |
| | 9 | | The type of the SignalR hub class to be mapped. |
| | 10 | | .PARAMETER Path |
| | 11 | | The URL path where the SignalR hub will be accessible. |
| | 12 | | .PARAMETER PassThru |
| | 13 | | If specified, the cmdlet will return the modified server instance after adding the SignalR hub. |
| | 14 | | .EXAMPLE |
| | 15 | | $server | Add-KrSignalRHub -HubType ([ChatHub]) -Path "/chat" |
| | 16 | | This example maps the ChatHub class to the "/chat" URL path on the specified Kestrun server. |
| | 17 | | .EXAMPLE |
| | 18 | | Get-KrServer | Add-KrSignalRHub -HubType ([ChatHub]) -Path "/chat" |
| | 19 | | This example retrieves the current Kestrun server and maps the ChatHub class to the "/chat" URL path. |
| | 20 | | .NOTES |
| | 21 | | This function is part of the Kestrun PowerShell module and is used to manage SignalR hubs on the Kestrun server. |
| | 22 | | The HubType parameter must be a valid SignalR hub class type. |
| | 23 | | The Path parameter specifies the URL path where the SignalR hub will be accessible. |
| | 24 | | The function uses reflection to find and invoke the generic AddSignalR<T>(string) method on the KestrunHost inst |
| | 25 | | This allows for dynamic mapping of SignalR hubs to specific URL paths at runtime. |
| | 26 | | The function returns the modified server instance for further chaining if needed. |
| | 27 | | The function ensures that the server instance is resolved before proceeding with the mapping. |
| | 28 | | The function is designed to be used in a pipeline, allowing for easy integration with other Kestrun commands. |
| | 29 | | The function is part of the Kestrun.Hosting namespace and is used to extend the functionality of the Kestrun ser |
| | 30 | | The function is designed to be used in a modular way, allowing for easy addition of SignalR hubs to the Kestrun |
| | 31 | | The function is intended for use in scenarios where SignalR hubs need to be dynamically mapped to specific URL p |
| | 32 | | The function is part of the Kestrun.Hosting library and is used to manage SignalR hubs on the Kestrun server. |
| | 33 | | The function is designed to be used in a modular way, allowing for easy addition of SignalR hubs to the Kestrun |
| | 34 | | The function is intended for use in scenarios where SignalR hubs need to be dynamically mapped to specific URL p |
| | 35 | | #> |
| | 36 | | function Add-KrSignalRHub { |
| | 37 | | [KestrunRuntimeApi('Definition')] |
| | 38 | | [CmdletBinding()] |
| | 39 | | param( |
| | 40 | | [Parameter(Mandatory = $true, ValueFromPipeline)] |
| | 41 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | 42 | |
|
| | 43 | | [Parameter(Mandatory)] |
| | 44 | | [Type]$HubType, |
| | 45 | |
|
| | 46 | | [Parameter(Mandatory)] |
| | 47 | | [string]$Path, |
| | 48 | |
|
| | 49 | | [Parameter()] |
| | 50 | | [switch]$PassThru |
| | 51 | | ) |
| | 52 | | begin { |
| | 53 | | # Ensure the server instance is resolved |
| 0 | 54 | | $Server = Resolve-KestrunServer -Server $Server |
| 0 | 55 | | if ($null -eq $Server) { |
| 0 | 56 | | throw 'Server is not initialized. Please ensure the server is configured before setting options.' |
| | 57 | | } |
| | 58 | | } |
| | 59 | | process { |
| 0 | 60 | | Write-KrLog -Level Warning 'Add-KrSignalRHub is an experimental feature and may not work as expected.' |
| | 61 | |
|
| | 62 | | # 1. Find the generic method definition on KestrunHost |
| 0 | 63 | | $method = $Server.GetType().GetMethods() | |
| 0 | 64 | | Where-Object { |
| 0 | 65 | | $_.Name -eq 'AddSignalR' -and |
| | 66 | | $_.IsGenericMethod -and |
| | 67 | | $_.GetParameters().Count -eq 1 # (string path) |
| | 68 | | } |
| | 69 | |
|
| 0 | 70 | | if (-not $method) { |
| 0 | 71 | | throw 'Could not locate the generic AddSignalR<T>(string) method.' |
| | 72 | | } |
| | 73 | |
|
| | 74 | | # 2. Close the generic with the hub type from the parameter |
| 0 | 75 | | $closed = $method.MakeGenericMethod(@($HubType)) |
| | 76 | |
|
| | 77 | | # 3. Invoke it, passing the path; return the resulting server for chaining |
| 0 | 78 | | $closed.Invoke($Server, @($Path)) | Out-Null |
| | 79 | |
|
| 0 | 80 | | if ($PassThru.IsPresent) { |
| | 81 | | # if the PassThru switch is specified, return the server instance |
| | 82 | | # Return the modified server instance |
| 0 | 83 | | return $Server |
| | 84 | | } |
| | 85 | | } |
| | 86 | | } |
| | 87 | |
|