Skip to content

Commit e2f05ee

Browse files
author
James Brundage
committed
feat: Get-WebSocket -Timeout/-LifeSpan server support ( Fixes #85 )
1 parent c81e6b9 commit e2f05ee

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

Commands/Get-WebSocket.ps1

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,9 @@ function Get-WebSocket {
184184

185185
# A collection of query parameters.
186186
# These will be appended onto the `-SocketUrl`.
187+
# Multiple values for a single parameter will be passed as multiple parameters.
187188
[Parameter(ValueFromPipelineByPropertyName,ParameterSetName='WebSocketClient')]
189+
[Alias('QueryParameters','Query')]
188190
[Collections.IDictionary]
189191
$QueryParameter,
190192

@@ -315,7 +317,7 @@ function Get-WebSocket {
315317
$values = $_.values
316318
foreach ($key in $keys) {
317319
if ($key -isnot [scriptblock]) {
318-
throw "Keys '$key' must be a scriptblock"
320+
throw "Key '$key' must be a scriptblock"
319321
}
320322
}
321323
foreach ($value in $values) {
@@ -329,7 +331,10 @@ function Get-WebSocket {
329331
[Collections.IDictionary]
330332
$WatchFor,
331333

332-
# The timeout for the WebSocket connection. If this is provided, after the timeout elapsed, the WebSocket will be closed.
334+
# The timeout for the WebSocket connection.
335+
# If this is provided, after the timeout elapsed, the WebSocket will be closed.
336+
[Parameter(ValueFromPipelineByPropertyName)]
337+
[Alias('Lifespan')]
333338
[TimeSpan]
334339
$TimeOut,
335340

@@ -810,12 +815,20 @@ function Get-WebSocket {
810815
"<style type='text/css'>$css</style>"
811816
}
812817
}
813-
)
818+
)
814819

815820
$httpListener.psobject.properties.add([psnoteproperty]::new('JobVariable',$Variable), $true)
821+
$listenerStartTime = [DateTime]::Now
816822

817823
# While the listener is listening,
818824
while ($httpListener.IsListening) {
825+
# If we've given a timeout for the listener,
826+
# and the listener has been open for longer than the timeout,
827+
if ($Timeout -and ([DateTime]::Now - $listenerStartTime) -gt $TimeOut) {
828+
# then it's closing time (you don't have to go home but you can't stay here).
829+
$httpListener.Stop()
830+
break
831+
}
819832
# get the context asynchronously.
820833
$contextAsync = $httpListener.GetContextAsync()
821834
# and wait for it to complete.

0 commit comments

Comments
 (0)