Skip to content

Instantly share code, notes, and snippets.

@snydergd
Created January 8, 2026 02:42
Show Gist options
  • Select an option

  • Save snydergd/a44e01dba60eb58e3f5207f8f58c617c to your computer and use it in GitHub Desktop.

Select an option

Save snydergd/a44e01dba60eb58e3f5207f8f58c617c to your computer and use it in GitHub Desktop.
Interact with websocket using PowerShell
# based on https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/websockets
# Very minimal setup without good error handling or careful consideration - for quick and dirty tests
$c = [System.Net.WebSockets.ClientWebSocket]::new()
$c.Options.KeepAliveInterval = [TimeSpan]::FromSeconds(10)
$c.Options.KeepAliveTimeout = [TimeSpan]::FromSeconds(10)
$c.connectasync("ws://localhost:8000/ws", [Activator]::CreateInstance([System.Threading.CancellationToken])).GetAwaiter().GetResult()
$b = [byte[]]::new(1024)
$result = $c.receiveAsync([System.ArraySegment[byte]]$b, [System.Threading.CancellationToken]::None).GetAwaiter().GetResult()
$text = [System.Text.Encoding]::UTF8.GetString($b, 0, $result.Count)
$c.CloseAsync([System.Net.WebSockets.WebSocketCloseStatus]::NormalClosure, "Client closed", [System.Threading.CancellationToken]::None).GetAwaiter().GetResult()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment