@@ -248,6 +248,10 @@ function Get-WebSocket {
248248 [long ]
249249 $Maximum ,
250250
251+ # The throttle limit used when creating background jobs.
252+ [int ]
253+ $ThrottleLimit = 64 ,
254+
251255 # The maximum time to wait for a connection to be established.
252256 # By default, this is 7 seconds.
253257 [TimeSpan ]
@@ -274,12 +278,12 @@ function Get-WebSocket {
274278 [Collections.IDictionary ]$Variable
275279 )
276280
281+ $Variable.JobRunspace = [Runspace ]::DefaultRunspace
282+
277283 # Take every every `-Variable` passed in and define it within the job
278284 foreach ($keyValue in $variable.GetEnumerator ()) {
279285 $ExecutionContext.SessionState.PSVariable.Set ($keyValue.Key , $keyValue.Value )
280- }
281-
282- $Variable.JobRunspace = [Runspace ]::DefaultRunspace
286+ }
283287
284288 if ((-not $WebSocketUri )) {
285289 throw " No WebSocketUri"
@@ -384,8 +388,7 @@ function Get-WebSocket {
384388 $SkipCount ++
385389 continue WebSocketMessageLoop
386390 }
387-
388-
391+
389392 $MessageObject
390393 if ($First -and ($MessageCount - $FilteredCount - $SkipCount ) -ge $First ) {
391394 $Maximum = $first
@@ -444,7 +447,7 @@ function Get-WebSocket {
444447 $ExecutionContext.SessionState.PSVariable.Set ($keyValue.Key , $keyValue.Value )
445448 }
446449
447- $Variable. JobRunspace = [Runspace ]::DefaultRunspace
450+ $Variable [ ' JobRunspace' ] = [Runspace ]::DefaultRunspace
448451
449452 # If we have routes, we will cache all of their possible parameters now
450453 if ($route.Count ) {
@@ -485,6 +488,7 @@ function Get-WebSocket {
485488
486489 # If the listener isn't listening, start it.
487490 if (-not $httpListener.IsListening ) { $httpListener.Start () }
491+ $httpListener.psobject.properties.add ([psnoteproperty ]::new(' JobVariable' , $Variable ), $true )
488492
489493 # While the listener is listening,
490494 while ($httpListener.IsListening ) {
@@ -738,6 +742,10 @@ function Get-WebSocket {
738742 }
739743
740744 $Variable [' MainRunspace' ] = [Runspace ]::DefaultRunspace
745+ $StartThreadJobSplat = [Ordered ]@ {
746+ InitializationScript = $InitializationScript
747+ ThrottleLimit = $ThrottleLimit
748+ }
741749
742750 # If we're going to be listening for HTTP requests, run a thread job for the server.
743751 if ($RootUrl ) {
@@ -776,11 +784,17 @@ function Get-WebSocket {
776784 $httpListenerJob = $existingJob
777785 $httpListener = $existingJob.HttpListener
778786 } else {
779- $httpListenerJob = Start-ThreadJob - ScriptBlock $SocketServerJob - Name " $RootUrl " - InitializationScript $InitializationScript - ArgumentList $Variable
780- }
787+ $httpListenerJob = Start-ThreadJob - ScriptBlock $SocketServerJob - Name " $RootUrl " - ArgumentList $Variable @StartThreadJobSplat
788+ }
781789 }
782790
791+ # If we have a listener job
783792 if ($httpListenerJob ) {
793+ # and the job has not started
794+ if ($httpListenerJob.JobStateInfo.State -eq ' NotStarted' ) {
795+ # sleep for no time (this will allow the job to start)
796+ Start-Sleep - Milliseconds 0
797+ }
784798 foreach ($keyValuePair in $Variable.GetEnumerator ()) {
785799 $httpListenerJob.psobject.properties.add (
786800 [psnoteproperty ]::new($keyValuePair.Key , $keyValuePair.Value ), $true
@@ -841,8 +855,8 @@ function Get-WebSocket {
841855 if ($existingJob -and -not $Force ) {
842856 $existingJob
843857 } else {
844- Start-ThreadJob - ScriptBlock $SocketClientJob - Name $Name - InitializationScript $InitializationScript - ArgumentList $Variable
845- }
858+ Start-ThreadJob - ScriptBlock $SocketClientJob - Name $Name - ArgumentList $Variable @StartThreadJobSplat
859+ }
846860 }
847861
848862 $subscriptionSplat = @ {
0 commit comments