Lightstreamer 创建会话
Posted
技术标签:
【中文标题】Lightstreamer 创建会话【英文标题】:Lightstreamer create a session 【发布时间】:2016-01-24 01:33:23 【问题描述】:我正在尝试使用 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);
我的问题是我正在创建一个会话,并确定我应该发送哪些信息以及如何发送。第 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】:如果你的应用是基于C#的,你不需要自己实现协议,但是如果你使用现成的客户端库会好很多。它为您提供更高级别的抽象,并且更加健壮且经过良好测试。
您可以从https://labs.ig.com/lightstreamer-downloads 下载客户端库并访问完整的文档和示例
您提到的网络协议教程仅在您需要基于任何可用 Lightstreamer 客户端库未涵盖的技术开发应用程序时才有用。
【讨论】:
以上是关于Lightstreamer 创建会话的主要内容,如果未能解决你的问题,请参考以下文章
如何确定 Lightstreamer / Ajax 彗星推送数据更改了哪些内容?