-
Notifications
You must be signed in to change notification settings - Fork 0
/
testwebsocket.ps1
64 lines (56 loc) · 2.03 KB
/
testwebsocket.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
55
56
57
58
59
60
61
62
63
$token = $Env:PROXY_TOKEN
$proxy_domain = $Env:PROXY_DOMAIN
$proxy_secure = $Env:PROXY_SECURE.Trim().ToLower().Trim("'").Trim('"')
if ($proxy_secure -eq "true") {
$proxy_secure = $true
} else {
$proxy_secure = $false
}
$proxy_ws = "ws://"
$proxy_http = "http://"
if ($proxy_secure) {
$proxy_ws = "wss://"
$proxy_http = "https://"
}
$uri = [System.Uri]::new($proxy_ws + $proxy_domain + "/wsfe/proxies/agents/connect")
Write-Host "URI: " $uri
$websocket = New-Object System.Net.WebSockets.ClientWebSocket
$websocket.Options.SetRequestHeader("Authorization", "Bearer $token")
# try {
# Write-Host "Trying to create websocket"
# $websocket.ConnectAsync($uri, [Threading.CancellationToken]::None).Wait()
# if ($websocket.State -eq [System.Net.WebSockets.WebSocketState]::Open) {
# Write-Host "Able to connect to websocket"
# }
# } catch {
# $errored = $true
# Write-Host "Error: $($_.Exception.Message)"
# Write-Host "URI: " $uri
# #Write-Host "StackTrace: $($_.Exception.StackTrace)"
# Write-Host "Unable to establish websocket connection."
# }
$itercount = 0
while ($itercount -lt 50) {
$errored = $false
try {
Write-Host "Trying to create websocket"
$websocket = New-Object System.Net.WebSockets.ClientWebSocket
$websocket.Options.SetRequestHeader("Authorization", "Bearer $token")
$websocket.ConnectAsync($uri, [Threading.CancellationToken]::None).Wait()
} catch {
$errored = $true
Write-Host "Error: $($_.Exception.Message)"
Write-Host "URI: " $uri
#Write-Host "StackTrace: $($_.Exception.StackTrace)"
Write-Host "Unable to establish websocket connection. Check A. Trying again."
}
if ($websocket.State -ne [System.Net.WebSockets.WebSocketState]::Open) {
$errored = $true
} else {
Write-Host "Able to connect. itercount: " $itercount
}
Write-Host "The value of errored:" $errored
Start-Sleep -Seconds 2
Write-Host "Done sleeping for 2 seconds"
$itercount = $itercount + 1
}