简体   繁体   中英

Lightstreamer create a session

I am trying to connect to IG index streaming API using LightStreamer , and am working on this tutorial http://www.lightstreamer.com/docs/client_generic_base/Network%20Protocol%20Tutorial.pdf

I have successfully created a streaming session(page 16 4.1 - /lightstreamer/create_session.txt), by doing the following:

public void ConnectToLightStreamer(Credentials credentials)
{
    var LightStreamerRequest = (HttpWebRequest)WebRequest.Create(credentials.LIGHTSTREAMERENDPOINT + "/lightstreamer/create_session.txt");
    LightStreamerRequest.Method = "POST";
    LightStreamerRequest.ContentType = "application/x-www-form-urlencoded";
    LightStreamerRequest.Headers.Add("Cache-Control", "no-cache");

    using (var streamWriter = new StreamWriter(LightStreamerRequest.GetRequestStream()))
    {
        string Body = "LS_user=" + credentials.CLIENTID + "&LS_password=CST-" + credentials.CST + "|XST-" + credentials.SECURITY_TOKEN + "&LS_adapter_set=DEFAULT&LS_polling=true&LS_polling_millis=0&LS_idle_millis=0&LS_report_info=true";

        streamWriter.Write(Body);
        streamWriter.Flush();
        streamWriter.Close();
    }

    var httpResponse = (HttpWebResponse)LightStreamerRequest.GetResponse();
    Stream stream = httpResponse.GetResponseStream();

    using (stream)
    {
        StreamReader reader = new StreamReader(stream, Encoding.UTF8);
        string responseString = reader.ReadToEnd();

        string[] SessionData = responseString.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

        string[] sessionID = SessionData[1].Split(':');
        credentials.SESSIONID = sessionID[1];

        string[] controlAddress = SessionData[2].Split(':');
        credentials.CONTROLADDRESS = controlAddress[1];

        Console.WriteLine(responseString);
        Console.WriteLine("CONNECTED");
        Console.WriteLine(Environment.NewLine);
        Console.WriteLine(Environment.NewLine);                
    }
}

I have managed to successfully bind the session(page 18/19, /lightstreamer/bind_session.txt) by doing the following:

 public void BindSession(Credentials credentials)
{
    var LightStreamerRequest = (HttpWebRequest)WebRequest.Create(credentials.LIGHTSTREAMERENDPOINT + "/lightstreamer/bind_session.txt");
    LightStreamerRequest.Method = "POST";
    LightStreamerRequest.ContentType = "application/x-www-form-urlencoded";
    LightStreamerRequest.Headers.Add("Cache-Control", "no-cache");

    using (var streamWriter = new StreamWriter(LightStreamerRequest.GetRequestStream()))
    {

        string Body = "LS_user=" + credentials.CLIENTID + "&LS_session=" + credentials.SESSIONID + "&LS_password=CST-" + credentials.CST + "|XST-" + credentials.SECURITY_TOKEN + "&LS_adapter_set=DEFAULT&LS_polling=true&LS_polling_millis=0&LS_idle_millis=0&LS_report_info=true&LS_polling_millis=1000";

        streamWriter.Write(Body);
        streamWriter.Flush();
        streamWriter.Close();
    }

    var httpResponse = (HttpWebResponse)LightStreamerRequest.GetResponse();
    Stream stream = httpResponse.GetResponseStream();

    using (stream)
    {
        StreamReader reader = new StreamReader(stream, Encoding.UTF8);

        string responseString = reader.ReadToEnd();

        Console.WriteLine("BIND: " + responseString);
    }
}

My problem I am having creating a session, and working out what information I am supposed to send and how. Page 20/21 /lightstreamer/control.txt.

I have the following, but I know it is wrong, as I am not getting any response:

public void ControlStreamer(Credentials credentials)
{
    var LightStreamerRequest = (HttpWebRequest)WebRequest.Create(credentials.LIGHTSTREAMERENDPOINT + "/lightstreamer/control.txt");
    LightStreamerRequest.Method = "POST";
    LightStreamerRequest.ContentType = "application/x-www-form-urlencoded";
    LightStreamerRequest.Headers.Add("Cache-Control", "no-cache");

    using (var streamWriter = new StreamWriter(LightStreamerRequest.GetRequestStream()))
    {

        string Body = "LS_user=" + credentials.CLIENTID + "&LS_password=CST-" + credentials.CST + "|XST-" + credentials.SECURITY_TOKEN + "&LS_session=" + credentials.SESSIONID + "&LS_polling=true&LS_polling_millis=0&LS_idle_millis=0&LS_report_info=true&LS_polling_millis=1000&LS_table=" + credentials.SESSIONID + "&LS_op=add&LS_schema=MARKET:IX.D.DOW.DAILY.IP&LS_id=1&LS_mode=MERGE";

        streamWriter.Write(Body);
        streamWriter.Flush();
        streamWriter.Close();
    }

    var httpResponse = (HttpWebResponse)LightStreamerRequest.GetResponse();
    Stream stream = httpResponse.GetResponseStream();

    using (stream)
    {
        StreamReader reader = new StreamReader(stream, Encoding.UTF8);

        string responseString = reader.ReadToEnd();

        Console.WriteLine("CONTROL: " + responseString);
    }
}

I have been a full time developer for about a year, and this project is at the very edge of my limit and understanding.

I have been doing this project for 2 full days +, but can barely understand the documentation.

If anyone is able to explain to me what I am doing wrong, or point me towards a working example, I would be very grateful.

If your application is based on C#, you don't need to implement the protocol yourself, but it's much better if you use the ready-made client library. It gives you a higher level of abstraction and it's more robust and well tested.

You can download the client library and access full documentation and examples from https://labs.ig.com/lightstreamer-downloads

The tutorial for the network protocol, which you mentioned, is only useful if you need to develop an application based on a technology not covered by any of the available Lightstreamer client libraries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM