VB.NET Client Application using Streaming API (Lightstreamer)

2 posts / 0 new
Last post
bellsy
VB.NET Client Application using Streaming API (Lightstreamer)

Hi, I a looking to build a simple client application in VB.NET

At the moment I am trying to login to the REST API, then use the obtained information to connect to the Streaming API (Lightstreamer). - to receive confirmation of connection before creating a subscription.

I have successfully logged in to the REST API, but I am unsure how to receive responses when logging in to the Streaming API. - See "Sub connectToLightstreamer" code.
I have included [COMMENTS] in my code below, if someone could take a look and offer advice or example VB.NET code it would help me a great deal.

Thanks, Colin.

CODE:

Dim m_strAPIURI As String = "https://demo-api.ig.com/gateway/deal"
Dim m_strAPIKey As String = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" '[VALID DEMO API KEY]
Dim m_strUser As String = "bellsydemo1" '[CORRECT DEMO USER]
Dim m_strPassword As String = "xxxxxxxxxxxx" '[CORRECT DEMO PASSWORD]

Dim m_strCST As String = ""
Dim m_strX_SECURITY_TOKEN As String = ""

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

' Create a request using a URL that can receive a post.
Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(m_strAPIURI & "/session")

' Set the Method property of the request to POST.
request.Method = "POST"
' Create POST data and convert it to a byte array.
Dim postData As String = "{" & vbCrLf & Chr(34) & "identifier" & Chr(34) & ": " & Chr(34) & m_strUser & Chr(34) & ", " & vbCrLf & Chr(34) & "password" & Chr(34) & ": " & Chr(34) & m_strPassword & Chr(34) & vbCrLf & "}"
Dim byteArray As Byte() = System.Text.Encoding.UTF8.GetBytes(postData)
' Set the ContentType property of the WebRequest.
request.ContentType = "application/json; charset=UTF-8"
request.Accept = "application/json; charset=UTF-8"
request.Headers.Add("X-IG-API-KEY", m_strAPIKey)

' Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length
' Get the request stream.
Dim dataStream As System.IO.Stream = request.GetRequestStream()
' Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length)
' Close the Stream object.
dataStream.Close()
' Get the response.
Dim response As System.Net.WebResponse = request.GetResponse()
' Display the status.
If CType(response, System.Net.HttpWebResponse).StatusCode = 200 Then
m_strCST = response.Headers("CST")
m_strX_SECURITY_TOKEN = response.Headers("X-SECURITY-TOKEN")
Call connectToLightstreamer(m_strCST, m_strX_SECURITY_TOKEN)
Label1.Text = "Logged In"
End If

'Console.WriteLine(CType(response, System.Net.HttpWebResponse).StatusDescription)
' Get the stream containing content returned by the server.
dataStream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New System.IO.StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
' Display the content.
'MsgBox(responseFromServer)
' Clean up the streams.
reader.Close()
dataStream.Close()
response.Close()
End Sub

Private Sub connectToLightstreamer(strCST As String, strX_SECURITY_TOKEN As String)

' Instantiate Lightstreamer client instance
Dim lsClient = New Lightstreamer.DotNet.Client.LSClient
Dim lsClientConn = New Lightstreamer.DotNet.Client.ConnectionInfo
Dim lsClientListen As Lightstreamer.DotNet.Client.IConnectionListener = Nothing

'Set up login credentials: client
'[Is the User Identifier - user name or the ID of an account such as a CFD account]
lsClientConn.User = "bellsydemo1"
lsClientConn.Password = "CST-" + strCST + "|XST-" + strX_SECURITY_TOKEN
'[THIS IS THE URL RETURNED BY THE REST API SUCCESSFUL LOGIN]
lsClientConn.PushServerUrl = "https://demo-apd.marketdatasystems.com"

'do i need a specific adapter for the IG Lightstreamer server?
'commented - no Adapter used at the moment
'lsClientConn.Adapter = "?????"

'[Should a Callback Function be added to the Listener to read responses from the server? - how in this achieved using VB.NET?]
lsClient.OpenConnection(lsClientConn, lsClientListen)

'[AT THIS POINT lsClient DOES SEEM TO HAVE MADE A CONNECTION AS VALUES CAN BE VIEWED IN DEBUG]
'[Can OnConnectionEstablished be used here - how would it be used?]
'lsClientListen.OnConnectionEstablished()

lsClient.CloseConnection()

End Sub

gb
my 2cts worth

Hi,
not sure if this is still up to date but I thought I'd give you a bit of experience. I'm new at this connection too and trying to build a C# app.
From my experience, you can connect without any adapter.
I implemented the interface for the listener and I received a successful answer when connecting the way your code does.

Let me know how things go

gb

Log in or register to post comments