forked from bitluni/ESP32CompositeVideo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket-test.ps1
54 lines (42 loc) · 1.69 KB
/
websocket-test.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Try{
Do{
$URL = 'ws://esp32.local:81'
$WS = New-Object System.Net.WebSockets.ClientWebSocket
$CT = New-Object System.Threading.CancellationToken
$WS.Options.UseDefaultCredentials = $true
#Get connected
$Conn = $WS.ConnectAsync($URL, $CT)
While (!$Conn.IsCompleted) {
Start-Sleep -Milliseconds 100
}
Write-Host "Connected to $($URL)"
$Size = 1024
$Array = [byte[]] @(,0) * $Size
#Send Starting Request
$Command = [System.Text.Encoding]::UTF8.GetBytes("ACTION=Command")
$Send = New-Object System.ArraySegment[byte] -ArgumentList @(,$Command)
$Conn = $WS.SendAsync($Send, [System.Net.WebSockets.WebSocketMessageType]::Text, $true, $CT)
While (!$Conn.IsCompleted) {
#Write-Host "Sleeping for 100 ms"
Start-Sleep -Milliseconds 100
}
Write-Host "Finished Sending Request"
#Start reading the received items
While ($WS.State -eq 'Open') {
$Recv = New-Object System.ArraySegment[byte] -ArgumentList @(,$Array)
$Conn = $WS.ReceiveAsync($Recv, $CT)
While (!$Conn.IsCompleted) {
#Write-Host "Sleeping for 100 ms"
Start-Sleep -Milliseconds 100
}
#Write-Host "Finished Receiving Request"
$stringresp = [System.Text.Encoding]::ASCII.GetString($Recv)
Write-Host $stringresp
}
} Until ($WS.State -ne 'Open')
}Finally{
If ($WS) {
Write-Host "Closing websocket"
$WS.Dispose()
}
}