Skip to content

Commit 4926a83

Browse files
author
James Brundage
committed
feat: Get-WebSocket -Force ( Fixes #58 )
Allowing -Force on HttpListener jobs
1 parent f216e42 commit 4926a83

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

Commands/Get-WebSocket.ps1

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ function Get-WebSocket {
191191
# If set, will output the raw bytes that come out of the WebSocket.
192192
[Alias('RawByte','RawBytes','Bytes','Byte')]
193193
[switch]
194-
$Binary,
194+
$Binary,
195195

196196
# If set, will force a new job to be created, rather than reusing an existing job.
197197
[switch]
@@ -383,7 +383,7 @@ function Get-WebSocket {
383383
}
384384

385385

386-
$MessageObject
386+
$MessageObject
387387
if ($First -and ($MessageCount - $FilteredCount - $SkipCount) -ge $First) {
388388
$Maximum = $first
389389
}
@@ -739,22 +739,42 @@ function Get-WebSocket {
739739
# If we're going to be listening for HTTP requests, run a thread job for the server.
740740
if ($RootUrl) {
741741

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
749753
}
750754
}
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+
}
752768

753769
if ($DebugPreference -notin 'SilentlyContinue','Ignore') {
754770
. $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+
}
758778

759779
if ($httpListenerJob) {
760780
foreach ($keyValuePair in $Variable.GetEnumerator()) {
@@ -763,7 +783,7 @@ function Get-WebSocket {
763783
)
764784
}
765785
$httpListenerJob
766-
}
786+
}
767787
}
768788

769789
# If `-Debug` was passed,

0 commit comments

Comments
 (0)