diff --git a/StackMonkey.ps1 b/StackMonkey.ps1 index fb9516c..0b283ca 100644 --- a/StackMonkey.ps1 +++ b/StackMonkey.ps1 @@ -311,6 +311,29 @@ } } +# Starts the HTTP listener loop + function Start-Server { + # make it accessible to Dispatch-Request + $Global:Listener = [System.Net.HttpListener]::new() + $Global:Listener.Prefixes.Add("http://localhost:$Port/") + $Global:Listener.Start() + Write-Host "Listening on http://localhost:$Port/ ..." + + try { + while ($Global:Listener.IsListening) { + $ctx = $Global:Listener.GetContext() + try { + Dispatch-Request $ctx + } catch { + Write-LogHybrid "Dispatch error: $_" "Error" "Server" + } + } + } finally { + # once the loop exits, clean up + $Global:Listener.Close() + Write-LogHybrid "Listener closed." "Info" "Server" + } + } #endregion region globalsetting @@ -1561,32 +1584,6 @@ $script Respond-Text $Context '404 - Not Found' } - - # Starts the HTTP listener loop - function Start-Server { - # make it accessible to Dispatch-Request - $Global:Listener = [System.Net.HttpListener]::new() - $Global:Listener.Prefixes.Add("http://localhost:$Port/") - $Global:Listener.Start() - Write-Host "Listening on http://localhost:$Port/ ..." - - try { - while ($Global:Listener.IsListening) { - $ctx = $Global:Listener.GetContext() - try { - Dispatch-Request $ctx - } catch { - Write-LogHybrid "Dispatch error: $_" "Error" "Server" - } - } - } finally { - # once the loop exits, clean up - $Global:Listener.Close() - Write-LogHybrid "Listener closed." "Info" "Server" - } - } - - #endregion