Lightstreamer创建一个会话

时间:2015-10-24 16:32:08

标签: c# .net lightstreamer

我正在尝试使用LightStreamer连接到IG索引流API,我正在处理这个教程http://www.lightstreamer.com/docs/client_generic_base/Network%20Protocol%20Tutorial.pdf

通过执行以下操作,我已成功创建了一个流会话(第16页4.1 - /lightstreamer/create_session.txt):

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);                
    }
}

通过执行以下操作,我成功地绑定了会话(第18/19页,/ lightstreamer / bind_session.txt):

 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);
    }
}

我的问题我正在创建会话,并计算出我应该发送的信息以及如何发送。 Page 20/21 /lightstreamer/control.txt。

我有以下内容,但我知道这是错误的,因为我没有得到任何回复:

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);
    }
}

我已经做了大约一年的全职开发人员,这个项目处于我的极限和理解的边缘。

我一直在做这个项目2天+,但几乎无法理解文档。

如果有人能够向我解释我做错了什么,或者指出我的工作实例,我将非常感激。

1 个答案:

答案 0 :(得分:3)

如果您的应用程序基于C#,您不需要自己实施协议,但如果您使用现成的客户端库,情况会好得多。它为您提供了更高级别的抽象,并且它更加强大且经过了充分测试。

您可以从https://labs.ig.com/lightstreamer-downloads

下载客户端库并访问完整文档和示例

您提到的网络协议教程仅在您需要基于任何可用的Lightstreamer客户端库未涵盖的技术开发应用程序时才有用。