参考文斯·派克(Vince Pike)在此处提供的解决方案: How to use a Websocket client to open a long-lived connection to a URL, using PowerShell V2?
发送文本效果很好。如何发送图像,例如PNG格式或二进制数据?
发送文本的示例来源:
$Size = 1024
$Array = [byte[]] @(,0) * $Size
$Message = "Sample Message"
$Command = [System.Text.Encoding]::UTF8.GetBytes($Message)
$Send = New-Object System.ArraySegment[byte] -ArgumentList @(,$Command)
$Conn = $WS.SendAsync($Send, [System.Net.WebSockets.WebSocketMessageType]::Text, $true, $CT)
While (!$Conn.IsCompleted) {
Start-Sleep -Milliseconds 100
}
答案 0 :(得分:0)
@Theo的评论解决了这个问题:
[Byte[]]$Command = [System.IO.File]::ReadAllBytes(<Absolute\Path\To\The\Image\Or\Binary\File>)
$Conn = $WS.SendAsync($Send, [System.Net.WebSockets.WebSocketMessageType]::Binary, $true, $CT)