我正在尝试使用套接字和服务器发送事件从tcp / ip服务器实时接收数据。
以下是一些代码:
public void ProcessRequest(HttpContext context)
{
HttpResponse Response = context.Response;
DateTime startDate = DateTime.Now;
Response.ContentType = "text/event-stream";
try
{
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
//Server is listening on port 1000
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 1000);
//Connect to the server
clientSocket.BeginConnect(ipEndPoint, new AsyncCallback(OnConnect), null);
}
catch (Exception ex)
{
// MessageBox.Show(ex.Message, "SGSclient", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
for (int i = 0; i < 50; i++)
{
try
{
Response.Write(string.Format("data: {0}\n\n", i));
System.Threading.Thread.Sleep(100);
Response.Flush();
}
catch
{
}
}
}
但是Callback方法不起作用。你能解释一下Server-Sent Events和SingalR来实时接收来自TCP / IP的数据吗?
答案 0 :(得分:0)
Microsoft ASP.NET SignalR 是ASP.NET开发人员的库,可简化向应用程序添加实时Web功能的过程
SingalR 工作正常。我找到了满足我需求的良好响应时间。