Powershell Websocket客户端发送图像

问题描述 投票:0回答:1

参考文斯·派克(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
}
image powershell websocket client send
1个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.