简体   繁体   English

如何使用套接字连接和服务器发送的事件实时从TCP / IP服务器接收数据到HTML页面?

[英]How to receive data to HTML page from TCP/IP server using socket connection and Server-Sent Events in real-time?

I am trying to receive data in real-time from tcp/ip server using socket and Server-Sent Events. 我正在尝试使用套接字和服务器发送事件从tcp / ip服务器实时接收数据。

Here is some code: 这是一些代码:

    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
        {

        }
    }
}

But Callback method is not working. 但是回调方法不起作用。 Could you please me explain about Server-Sent Events and SingalR to receive data from TCP/IP in real-time? 您能否请我解释一下有关服务器发送事件和SingalR以便实时从TCP / IP接收数据的信息?

Microsoft ASP.NET SignalR is a library for ASP.NET developers that simplifies the process of adding real-time web functionality to your applications Microsoft ASP.NET SignalR是ASP.NET开发人员的库,可简化向应用程序添加实时Web功能的过程

SingalR is working perfect. SingalR运行完美。 I found good response time for my needs. 我找到了可以满足自己需求的快速响应时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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