2016-09-18 76 views
6

我实际上在一个Web应用程序(ASP.NET MVC)上工作,它向TCP/IP中的本地网络上的胖客户端应用程序(我无法修改)发送命令。我需要胖客户端应用程序响应我的命令或给我提供信息,因此连接需要始终打开。我创建了一个循环,检查一样,服务器响应:TCP/IP客户端循环.NET

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); 
      } 
     } 

这是实际工作,我收到了来自服务器的响应,但浏览器是“等待localhost”的所有时间,所以有没有更好的方法来做到这一点,没有在浏览器上的循环?

非常感谢!

回答

5

这听起来像是一个真正的websocket案例。在MVC应用程序中,您通常会使用SignalR:http://www.asp.net/signalr

+0

首先感谢您的时间@percleish。我还有一个问题(也许是一个愚蠢的问题......)Web应用程序将被“部署”在服务器上,只能在没有互联网的本地网络上使用。这是使用信号发生器的问题吗? –

+0

不,只要将SignalR软件包部署到您的Intranet服务器,这应该无关紧要。如果你已经通过NuGet安装('Install-Package Microsoft.AspNet.SignalR'),那么它应该被正确打包/部署 – dperish