Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCP/IP Client side loop .NET

I actually work on a web application (ASP.NET MVC) which sends command to a thick-client application (that I can not modify) on a local network in TCP/IP. I need that the thick-client application respond to my command or give me information, so the connection need to be open all the time. I create a loop that checks the server response like that :

public static async void getMessage()
        {
            while(true)
            {
                // Buffer to store the response bytes.
                Byte[] data = new Byte[100000];

                // String to store the response ASCII representation.
                String responseData = String.Empty;

                // Read the first batch of the TcpServer response bytes.
                Int32 bytes = await stream.ReadAsync(data, 0, data.Length);
                if (bytes == 0) continue; 
                responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                Debug.Write(responseData);

                await stream.ReadAsync(data, 0, data.Length);
            }
        }

That's actually working, and I received the response from the server, but the browser is "waiting for localhost" all the time, so is there a better way to do that, without the loop on the browser?

Thank you a lot !

like image 253
Atloka Avatar asked Sep 18 '16 14:09

Atloka


1 Answers

This sounds like a really good case for a websocket. In an MVC app, you'd typically use SignalR: http://www.asp.net/signalr

like image 120
dperish Avatar answered Nov 07 '22 09:11

dperish