@@ -191,7 +191,7 @@ function Get-WebSocket {
191
191
# If set, will output the raw bytes that come out of the WebSocket.
192
192
[Alias (' RawByte' , ' RawBytes' , ' Bytes' , ' Byte' )]
193
193
[switch ]
194
- $Binary ,
194
+ $Binary ,
195
195
196
196
# If set, will force a new job to be created, rather than reusing an existing job.
197
197
[switch ]
@@ -383,7 +383,7 @@ function Get-WebSocket {
383
383
}
384
384
385
385
386
- $MessageObject
386
+ $MessageObject
387
387
if ($First -and ($MessageCount - $FilteredCount - $SkipCount ) -ge $First ) {
388
388
$Maximum = $first
389
389
}
@@ -739,22 +739,42 @@ function Get-WebSocket {
739
739
# If we're going to be listening for HTTP requests, run a thread job for the server.
740
740
if ($RootUrl ) {
741
741
742
- $variable [' HttpListener' ] = $httpListener = [Net.HttpListener ]::new()
743
- foreach ($potentialPrefix in $RootUrl ) {
744
- if ($potentialPrefix -match ' ^https?://' ) {
745
- $httpListener.Prefixes.Add ($potentialPrefix )
746
- } else {
747
- $httpListener.Prefixes.Add (" http://$potentialPrefix /" )
748
- $httpListener.Prefixes.Add (" https://$potentialPrefix /" )
742
+ if (-not $Name ) {
743
+ $Name = " $ ( $RootUrl -join ' |' ) "
744
+ }
745
+
746
+ $existingJob = foreach ($jobWithThisName in (Get-Job - Name $Name - ErrorAction Ignore)) {
747
+ if (
748
+ $jobWithThisName.State -in ' Running' , ' NotStarted' -and
749
+ $jobWithThisName.HttpListener -is [Net.HttpListener ]
750
+ ) {
751
+ $jobWithThisName
752
+ break
749
753
}
750
754
}
751
- $httpListener.Start ()
755
+
756
+ if ((-not $existingJob ) -or $Force ) {
757
+ $variable [' HttpListener' ] = $httpListener = [Net.HttpListener ]::new()
758
+ foreach ($potentialPrefix in $RootUrl ) {
759
+ if ($potentialPrefix -match ' ^https?://' ) {
760
+ $httpListener.Prefixes.Add ($potentialPrefix )
761
+ } else {
762
+ $httpListener.Prefixes.Add (" http://$potentialPrefix /" )
763
+ $httpListener.Prefixes.Add (" https://$potentialPrefix /" )
764
+ }
765
+ }
766
+ $httpListener.Start ()
767
+ }
752
768
753
769
if ($DebugPreference -notin ' SilentlyContinue' , ' Ignore' ) {
754
770
. $SocketServerJob - Variable $Variable
755
- } else {
756
- $httpListenerJob = Start-ThreadJob - ScriptBlock $SocketServerJob - Name " $RootUrl " - InitializationScript $InitializationScript - ArgumentList $Variable
757
- }
771
+ } else {
772
+ if ($existingJob -and -not $Force ) {
773
+ $httpListenerJob = $existingJob
774
+ } else {
775
+ $httpListenerJob = Start-ThreadJob - ScriptBlock $SocketServerJob - Name " $RootUrl " - InitializationScript $InitializationScript - ArgumentList $Variable
776
+ }
777
+ }
758
778
759
779
if ($httpListenerJob ) {
760
780
foreach ($keyValuePair in $Variable.GetEnumerator ()) {
@@ -763,7 +783,7 @@ function Get-WebSocket {
763
783
)
764
784
}
765
785
$httpListenerJob
766
- }
786
+ }
767
787
}
768
788
769
789
# If `-Debug` was passed,
0 commit comments