How to retrieve data require authorisation? VB.NET

edited June 2016 in General

I am planning to build our own bot using VB.NET. I have been succeeded to modify the code sample of C# to VB.NET and make it work.
I have established the connection, but facing issue in retrieving data that required authentication (Balance for example). I was successful to authorise using machine token, but when send 2nd request of retrieving Balance, I am getting no results.
I am wondering if I am missing anything here.

In other words, for the first request I am getting the response. But 2nd and later I am not getting responses.
I am using Async Tasks as per the sample code.

Answers

  • Hello. Can you show part of your source code?

    Regards

  • edited June 2016

    Hi,
    Thanks for the prompt reply.
    Its based on the code sample provided in C#. I have created console application with 2 modules:

    1. Module Main.vb

    Module Main
    Sub Main()
    MainLoop()
    End Sub
    Function MainLoop() As Boolean

                Dim TokenKey As String = "---KEY TO BE PLACED HERE---" ' <----- Enter TokeyKey here
    
                Dim data1 As String = "{""authorize"":""" + TokenKey + """}"
    
                Dim data2 As String = "{""get_limits"": ""1""}"
    
                Dim data3 As String = "{""ticks"": ""R_50""}"
    
                Dim bws = New BinaryWSDemo.BinaryWS()
                bws.Connect().Wait(100)
    
                bws.SendRequest(data1).Wait(100)
                bws.StartListen().Wait(1000)
    
                bws.SendRequest(data2).Wait(100)
                bws.StartListen()
    
                bws.SendRequest(data3)
                bws.StartListen()
    
            Console.ReadLine()
    
             Return True
    
        End Function
    

    End Module

    1. Module BinadyWS.vb

    Imports System.Text
    Imports System.Threading.Tasks
    Imports System.Net.WebSockets
    Imports System.Threading
    Imports System.Net

    Namespace BinaryWSDemo
    Class BinaryWS
    Private ws As New ClientWebSocket()
    Private uri As New Uri("wss://ws.binaryws.com/websockets/v3")

        Public Async Function SendRequest(data As String) As Task
    
            While Me.ws.State = WebSocketState.Connecting
            End While
    
            If Me.ws.State <> WebSocketState.Open Then
                Console.WriteLine("Connection is not open.")
                Await Me.Connect
                Throw New Exception("Connection is not open.")
            End If
    
            Dim reqAsBytes = Encoding.UTF8.GetBytes(data)
            Dim ticksRequest = New ArraySegment(Of Byte)(reqAsBytes)
    
            Await Me.ws.SendAsync(ticksRequest, WebSocketMessageType.Text, True, CancellationToken.None)
    
            Console.WriteLine("The request has been sent: ")
            Console.WriteLine(data)
            Console.WriteLine(vbCr & vbLf & " " & vbCr & vbLf)
    
        End Function
    
    Public Async Function GetBalance(data As String, data2 As String) As Task
    
            While Me.ws.State = WebSocketState.Connecting
            End While
    
            If Me.ws.State <> WebSocketState.Open Then
                Console.WriteLine("Connection is not open.")
                Throw New Exception("Connection is not open.")
            End If
    
            Dim reqAsBytes = Encoding.UTF8.GetBytes(data)
            Dim ticksRequest = New ArraySegment(Of Byte)(reqAsBytes)
            Await Me.ws.SendAsync(ticksRequest, WebSocketMessageType.Text, True, CancellationToken.None)
    
            If Me.ws.State <> WebSocketState.Open Then
                Console.WriteLine("Connection 2nd is not open.")
                Throw New Exception("Connection is not open.")
            End If
            Dim reqAsBytes2 = Encoding.UTF8.GetBytes(data2)
            Dim ticksRequest2 = New ArraySegment(Of Byte)(reqAsBytes)
            Await Me.ws.SendAsync(ticksRequest2, WebSocketMessageType.Text, True, CancellationToken.None)
    
            Console.WriteLine("The request has been sent: ")
            Console.WriteLine(data)
            Console.WriteLine(vbCr & vbLf & " " & vbCr & vbLf)
    
        End Function
    
        Public Async Function StartListen() As Task
            Dim result As WebSocketReceiveResult
            If Me.ws.State <> WebSocketState.Open Then
                Console.WriteLine("Connection is not open.")
                Throw New Exception("Connection is not open.")
            End If
    
            While Me.ws.State = WebSocketState.Open
                Dim buffer = New ArraySegment(Of Byte)(New Byte(1023) {})
                Do
                    result = Await Me.ws.ReceiveAsync(New ArraySegment(Of Byte)(buffer.Array), CancellationToken.None)
    
                    If result.MessageType = WebSocketMessageType.Close Then
                        Console.WriteLine("Connection Closed!")
                        Exit Do
                    Else
                        Dim str = Encoding.UTF8.GetString(buffer.Array, 0, result.Count)
                        Console.WriteLine("Received Data at: " + DateTime.Now)
                        Console.WriteLine(str)
                        Console.WriteLine(vbCr & vbLf)
                End If
                Loop While Not result.EndOfMessage
            End While
        End Function
    
        Public Async Function Connect() As Task
            Console.WriteLine("Prepare to connect to: " + Me.uri.ToString())
            Console.WriteLine(vbCr & vbLf)
    
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 Or SecurityProtocolType.Tls11
            Await ws.ConnectAsync(uri, CancellationToken.None)
    
            Console.WriteLine("The connection is established!")
            Console.WriteLine(vbCr & vbLf)
    
        End Function
    

    End Class

    End Namespace

  • Hello. That Class is not the best implementation for Binary Web Socket API. I recommed you to implement WebSocket4Net Library. Its very easy to use.

    Regards

  • edited June 2016

    Thanks for the reply.
    Can I find that class in NuGet ??

  • Yes of course.

    Regards

Sign In or Register to comment.