2016-09-18 100 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