Вверх ↑
Ответов: 194
Рейтинг: 27
#1: 2021-07-13 08:22:13 ЛС | профиль | цитата
Pavel, на сколько я понял, получить данные через websocket можно как-то так
Add(MainForm,2953706,70,140)
{
Caption="Test WebSocket Connections"
link(onCreate,12356033:doString,[])
}
Add(FormatStr,12356033,119,154)
{
Mask="$client_id = [System.GUID]::NewGuid()\r\n\r\n$recv_queue = New-Object 'System.Collections.Concurrent.ConcurrentQueue[String]'\r\n$send_queue = New-Object 'System.Collections.Concurrent.ConcurrentQueue[String]'\r\n\r\n$ws = New-Object Net.WebSockets.ClientWebSocket\r\n$cts = New-Object Threading.CancellationTokenSource\r\n$ct = New-Object Threading.CancellationToken($false)\r\n\r\n$connectTask = $ws.ConnectAsync("%1", $cts.Token)\r\n\r\n\r\ndo { Sleep(1) }\r\nuntil ($connectTask.IsCompleted)\r\n\r\n$recv_job = {\r\n param($ws, $client_id, $recv_queue)\r\n\r\n $buffer = [Net.WebSockets.WebSocket]::CreateClientBuffer(1024,1024)\r\n $ct = [Threading.CancellationToken]::new($false)\r\n $taskResult = $null\r\n\r\n while ($ws.State -eq [Net.WebSockets.WebSocketState]::Open) {\r\n $jsonResult = ""\r\n do {\r\n $taskResult = $ws.ReceiveAsync($buffer, $ct)\r\n while (-not $taskResult.IsCompleted -and $ws.State -eq [Net.WebSockets.WebSocketState]::Open) {\r\n [Threading.Thread]::Sleep(10)\r\n }\r\n\r\n $jsonResult += [Text.Encoding]::UTF8.GetString($buffer, 0, $taskResult.Result.Count)\r\n } until (\r\n $ws.State -ne [Net.WebSockets.WebSocketState]::Open -or $taskResult.Result.EndOfMessage\r\n )\r\n\r\n if (-not [string]::IsNullOrEmpty($jsonResult)) {\r\n #"Received message(s): $jsonResult" | Out-File -FilePath "logs.txt" -Append\r\n $recv_queue.Enqueue($jsonResult)\r\n }\r\n }\r\n }\r\n\r\n $send_job = {\r\n param($ws, $client_id, $send_queue)\r\n\r\n $ct = New-Object Threading.CancellationToken($false)\r\n $workitem = $null\r\n while ($ws.State -eq [Net.WebSockets.WebSocketState]::Open){\r\n if ($send_queue.TryDequeue([ref] $workitem)) {\r\n #"Sending message: $workitem" | Out-File -FilePath "logs.txt" -Append\r\n\r\n [ArraySegment[byte]]$msg = [Text.Encoding]::UTF8.GetBytes($workitem)\r\n $ws.SendAsync(\r\n $msg,\r\n [System.Net.WebSockets.WebSocketMessageType]::Binary,\r\n $true,\r\n $ct\r\n ).GetAwaiter().GetResult() | Out-Null\r\n }\r\n }\r\n }\r\n\r\n$recv_runspace = [PowerShell]::Create()\r\n$recv_runspace.AddScript($recv_job).\r\n AddParameter("ws", $ws).\r\n AddParameter("client_id", $client_id).\r\n AddParameter("recv_queue", $recv_queue).BeginInvoke() | Out-Null\r\n\r\n$send_runspace = [PowerShell]::Create()\r\n$send_runspace.AddScript($send_job).\r\n AddParameter("ws", $ws).\r\n AddParameter("client_id", $client_id).\r\n AddParameter("send_queue", $send_queue).BeginInvoke() | Out-Null\r\n\r\ntry {\r\n do {\r\n $msg = $null\r\n while ($recv_queue.TryDequeue([ref] $msg)) {\r\n $msg \r\n $hash = @{\r\n ClientID = $client_id\r\n Payload = "Wat"\r\n }\r\n\r\n $test_payload = New-Object PSObject -Property $hash\r\n $json = ConvertTo-Json $test_payload\r\n $send_queue.Enqueue($json)\r\n }\r\n } until ($ws.State -ne [Net.WebSockets.WebSocketState]::Open)\r\n}\r\nfinally {\r\n $closetask = $ws.CloseAsync(\r\n [System.Net.WebSockets.WebSocketCloseStatus]::Empty,\r\n "",\r\n $ct\r\n )\r\n\r\n do { Sleep(1) }\r\n until ($closetask.IsCompleted)\r\n $ws.Dispose()\r\n\r\n $recv_runspace.Stop()\r\n $recv_runspace.Dispose()\r\n\r\n $send_runspace.Stop()\r\n $send_runspace.Dispose()\r\n}"
link(onFString,1335603:doReplace,[])
link(Str1,3081994:Text,[])
}
Add(WinExec,2400366,217,140)
{
FileName="POWERSHELL"
Mode=0
Action="runas"
Point(doConsoleExec)
Point(onConsoleResult)
Point(onConsoleTerminate)
link(onConsoleResult,7504290:doCharset,[])
link(onConsoleTerminate,2953706:doClose,[(256,167)(256,195)(60,195)(60,167)])
}
Add(Replace,1335603,168,154)
{
SubStr="""
DestStr="'"
link(onReplace,2400366:doConsoleExec,[])
}
Add(Charset,7504290,280,154)
{
link(onCharset,9397794:doAdd,[])
}
Add(Memo,9397794,329,154)
{
Left=550
Top=160
Align=5
ScrollBars=3
}
Add(LinkTip,1754118,63,63)
{
Link="https://docs.binance.org/api-reference/dex-api/ws-connection.html"
Font=[MS Sans Serif,8,0,16711680,1]
}
Add(VisualStrings,15578961,133,21)
{
Lines=#46:wss://stream.binance.com:9443/ws/bnbbtc@ticker|
Width=333
}
Add(LinkTip,8580031,63,77)
{
Link="https://github.com/binance/binance-spot-api-docs/blob/master/web-socket-streams.md"
Font=[MS Sans Serif,8,0,16711680,1]
}
Add(VisualStrings,3081994,119,105)
{
Lines=#52:wss://testnet-dex.binance.org/api/ws/$all@allTickers|
Width=270
}


После запуска нужно немного подождать для устаноки соединения.
карма: 5

1
Голосовали:Pavel
Редактировалось 3 раз(а), последний 2021-07-13 08:54:39