2017-03-20 5 views
0

httpでクライアントにデータを継続的にストリーム配信しようとしています。このコードはlocalhost上で動作しますが、IISまたはApache環境(Linuxの場合はモノラル環境)では動作しません。ASP.NET MVC 5レスポンスストリーミングが機能しません。ステージング/プロダクション環境のみ

コードが本番サーバー上にある場合、接続を閉じるまで、コードは何もフラッシュされません。 localhost上で動作するように動作します。

public class EventsStreamController : Controller 
    { 
    static EventsStreamController() 
    { 
     ConcurrentDictionary = new ConcurrentDictionary<uint, Client>(); 
    } 

    private static ConcurrentDictionary<uint, Client> ConcurrentDictionary { get; } 

    /// <summary> 
    /// http://localhost:42022/EventsStream/SendMesaage/?pollSessionID=2&message=cats 
    /// </summary> 
    [HttpGet] 
    public async Task Connect(uint pollSessionID) 
    { 
     var httpResponse = this.Response; 

     httpResponse.BufferOutput = false; 

     var client = new Client(pollSessionID); 

     if (!ConcurrentDictionary.TryAdd(client.ClientId, client)) 
     { 
      throw new ApplicationException("The client is already in the dictionary."); 
     } 

     while (true) 
     { 
      await Task.Delay(2000); 

      string message; 

      if (client.Messages.TryDequeue(out message)) 
      { 
       byte[] buffer = Encoding.UTF8.GetBytes(message); 

       httpResponse.BinaryWrite(buffer); 
       httpResponse.Flush(); 

       if (message == "close") 
       { 
        break; 
       } 
      } 
     } 
    } 

    /// <summary> 
    /// http://localhost:42022/EventsStream/connect/?pollSessionID=2 
    /// </summary> 
    [HttpGet] 
    public ActionResult SendMesaage(uint pollSessionID, string message) 
    { 
     Client client; 
     if (!ConcurrentDictionary.TryGetValue(pollSessionID, out client)) 
     { 
      throw new ApplicationException("Client not found."); 
     } 

     client.Messages.Enqueue(message); 

     return new HttpStatusCodeResult(HttpStatusCode.OK); 
    } 

    public class Client 
    { 
     public Client(uint clientId) 
     { 
      this.ClientId = clientId; 
      this.Messages = new ConcurrentQueue<string>(); 
     } 

     public uint ClientId { get; set; } 

     public ConcurrentQueue<string> Messages { get; } 
    } 
} 

私はそれが私が簡略化のために2秒ごとにチェックし、それを変更しますが、クライアントにデータをストリーミングするために持って待機要求を通知するTaskCompletionSourceを使用していた。(また、「サーバー上で、まだdoesnのことをテスト

答えて

関連する問題