Created
January 8, 2026 02:42
-
-
Save snydergd/a44e01dba60eb58e3f5207f8f58c617c to your computer and use it in GitHub Desktop.
Interact with websocket using PowerShell
This file contains hidden or 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
| # 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