简体   繁体   中英

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.

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?

Microsoft ASP.NET SignalR is a library for ASP.NET developers that simplifies the process of adding real-time web functionality to your applications

SingalR is working perfect. I found good response time for my needs.

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