Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to receive data in real-time from tcp/ip server using socket and Server-Sent Events.
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 SingleR to receive data from TCP/IP in real-time?
Posted

1 solution

HTTP is stateless, ASP.NET WebForms only tries to hide this with viewstate and such. As soon as you see the page in your browser, the page isn't alive anymore and your OnSend callback won't be called anymore. This is also explained in Create web page with live socket based data.

If your goal is to update an HTML element with server-fed data, take a look at SignalR.

http://stackoverflow.com/questions/21870740/how-to-listen-socket-in-single-asp-net-page/21873956?noredirect=1#comment33133934_21873956[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900