Real-time-finance / finance-websocket-API

Public websocket API to get datas from financial markets

Home Page:https://realtimefinance.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Powershell Example

LxLeChat opened this issue · comments

Here is a Powershell example to connect to your websocket :)

$ws = New-Object System.Net.WebSockets.ClientWebSocket
$ct = New-Object System.Threading.CancellationToken
$ws.Options.SetRequestHeader('API-X-KEY',"demo")
$ws.Options.SetRequestHeader('symbol',"EURUSD")

## Connect
$connexion = $ws.ConnectAsync("ws://realtimefinance.io",$ct)
while( -not $connexion.IsCompleted ) {
    start-sleep -Milliseconds 10
}

$Size = 1024
$Array = [byte[]] @(,0) * $Size
$Recv = New-Object System.ArraySegment[byte] -ArgumentList @(,$Array)

## While state is open
while ( $ws.State -eq "Open") {
    $NewMessage = ""

    ## Receive Message
    Do {
        $Conn = $WS.ReceiveAsync($Recv, $CT)
        While (!$Conn.IsCompleted) { Start-Sleep -Milliseconds 10 }

        $Recv.Array[0..($Conn.Result.Count - 1)] | ForEach { $NewMessage += [char]$_ }

    } Until ($Conn.Result.Count -lt $Size)

    if ( $NewMessage ) {
        Try {
            $NewMessage | ConvertFrom-Json -ErrorAction Stop
        } Catch {
            $NewMessage
        }
    }
}

## Websocket state not open
Write-Host "Websocket disconnected..."
$Ws.Dispose()

Thanks you very much !
We will add it to documentation.