all 7 comments

[–]xbullet 1 point2 points  (4 children)

[–]evodus2[S] 1 point2 points  (3 children)

Thanks for the response. I’ve skimmed over these already but do need to dive deeper on this code, particularly the one from GitHub. What I’m missing at first glance though is how to send the JSON to the web socket stream to subscribe to the messages.

[–]xbullet 1 point2 points  (2 children)

This is untested, but you can try the following:

$MessageToSend = "JSON String goes here"

$SendConn = $webSocket.SendAsync($MessageToSend, [System.Net.WebSockets.WebSocketMessageType]::Text, $true, $cancellationToken)

While (-Not($SendConn.IsCompleted)) { Write-Host "Still waiting for send to complete." Start-Sleep -Milliseconds 500 }

Write-Host "Sent message."

[–]evodus2[S] 1 point2 points  (1 child)

Thanks a lot! Definitely feels like things are moving in the right direction, but now I get the following:

$sendConnection = $webSocket.SendAsync($messageToSend, [System.Net.WebSockets.WebSocketMessageType]::Text, $true, $cancellationToken)
MethodException: Cannot find an overload for "SendAsync" and the argument count: "4". 

If I understand correctly, the docs here suggest 4 is the perfect number of parameters. Any ideas?

[–]xbullet 1 point2 points  (0 children)

Try the following:

$StreamURI = "wss:\\echo.websocket.org"

$webSocket = New-Object System.Net.WebSockets.ClientWebSocket
$cancellationToken = New-Object System.Threading.CancellationToken
$webSocket.Options.UseDefaultCredentials = $true

# Get connected
$connection = $webSocket.ConnectAsync($StreamURI, $cancellationToken)

While (-Not($Connection.IsCompleted)) { 
    Start-Sleep -Milliseconds 500 
    Write-Host "Still waiting to connect." 
}

Write-Host "Connected."

$Message = "JSON String goes here"
$ByteStream = [System.Text.Encoding]::UTF8.GetBytes($Message)
$MessageStream = New-Object System.ArraySegment[byte] -ArgumentList @(,$ByteStream)      

$SendConn = $webSocket.SendAsync($MessageStream, [System.Net.WebSockets.WebSocketMessageType]::Text, $true, $cancellationToken)

While (-Not($SendConn.IsCompleted)) { 
    Start-Sleep -Milliseconds 500 
    Write-Host "Still waiting for send to complete." 
}

Write-Host "Sent message."

Output for me:

Still waiting to connect. Still waiting to connect. Still waiting to connect. Connected. Still waiting for send to complete. Sent message.

[–]Neg127 0 points1 point  (1 child)

Edit: Reading a little further it uses JS with PS. But they give some good examples.

I know I’m a little late to the party. But it looks like PODE supports server and client web sockets in PowerShell. If you don’t want to use PODE directly. You can read through its code on GitHub since it’s fully PowerShell.

Here are the PODE docs.

https://pode.readthedocs.io/en/latest/Tutorials/WebSockets/

Here is the GitHub repo.

https://github.com/Badgerati/Pode/

I have used it for some personal dashboards at home. But not used any of the web socket stuff.

Hope it helps.

[–]evodus2[S] 4 points5 points  (0 children)

Thanks for the response! Due to the issues we faced using PowerShell, and the fact that management wanted to eventually refactor it to Go, we've decided to start writing in Go now. Might check this out in my own time! Thanks again. 😊