-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
function Log { | ||
param( | ||
[ArgumentCompletions('Listener', 'Response', 'Context', 'Other', 'Task')] | ||
[Parameter()] | ||
$Who, | ||
[ArgumentCompletions('ctor', 'Close', 'Start', 'End', 'Wait', 'Sleep')] | ||
[Parameter()] | ||
$What, | ||
[Parameter()] | ||
$Message = '' | ||
) | ||
$Color = switch ($Who) { | ||
'Listener' { 'lightblue' } | ||
'Context' { 'lightgreen' } | ||
'Response' { 'orange' } | ||
default { 'darkyellow' } | ||
} | ||
"${Who}::${What} ${Message}" | Write-Host -bg $Color -fg black | ||
} | ||
|
||
'tip: Open a new window and run | ||
> irm http://localhost:9092 | ||
' | write-host -fore red | ||
|
||
if( $listen.IsListening ) { | ||
throw 'nope' | ||
# or | ||
$Listen.Dispose() | ||
$Listen = $Null | ||
} | ||
|
||
[Net.HttpListener]$Listen = @{} | ||
$listen.Prefixes.add('http://localhost:9098/') | ||
$Listen.Start() | ||
|
||
Log Task Start 'Async Wait' | ||
$task = $Listen.GetContextAsync() | ||
try { | ||
while ( -not $Task.AsyncWaitHandle.WaitOne(200)) { | ||
Log Task Sleep 'tick 200ms' | ||
} | ||
$ctx = $task.GetAwaiter().GetResult() | ||
$ctx.Response.StatusCode = 200 | ||
$ctx.Response.ContentType = 'text/html' | ||
|
||
$message = 'hi world 🐒 !' | ||
$byte_str = [Text.Encoding]::UTF8.GetBytes( $message ) | ||
$json = ($ctx.Response | ConvertTo-Json -wa 0 -Depth 0 -Compress) | ||
Log Response Write $json | ||
$ctx.Response.OutputStream.Write( $byte_str, 0, $byte_str.Length ) | ||
|
||
} finally { | ||
Log Response Close | ||
Log Listen Close | ||
$ctx.Response.Dispose() | ||
$listen.Dispose() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
## Web related scripts | ||
|
||
- [Net.HttpListener Basics.ps1](Net.HttpListener%20Basics.ps1) | ||
- [Net.HttpListener AsyncWait And Safe Cancelation](./Net.HttpListener%20Part2%20Async%20Cancel.ps1) | ||
|
||
![screenshot](./img/Net.HttpListener-Basics.png) |