3

Webチャットを実装したい。

バックエンドはデュアル WCF チャネルです。デュアル チャネルはコンソールまたは winform で動作し、実際には Web でも動作します。少なくともメッセージの送受信はできます。

このブログ投稿をベースとして使用し たため、非同期操作が完了しました。

結果をデバッグすると、メッセージがすべてブラウザに送信できる状態になっていることがわかります。

[AsyncTimeout(ChatServer.MaxWaitSeconds * 1020)] // timeout is a bit longer than the internal wait
public void IndexAsync()
{
  ChatSession chatSession = this.GetChatSession();
  if (chatSession != null)
  {
    this.AsyncManager.OutstandingOperations.Increment();
    try
    {
      chatSession.CheckForMessagesAsync(msgs =>
      {
        this.AsyncManager.Parameters["response"] = new ChatResponse { Messages = msgs };
        this.AsyncManager.OutstandingOperations.Decrement();
      });
    }
    catch (Exception ex)
    {
      Logger.ErrorException("Failed to check for messages.", ex);
    }
  }
}

public ActionResult IndexCompleted(ChatResponse response)
{
  try
  {
    if (response != null)
    {
      Logger.Debug("Async request completed. Number of messages: {0}", response.Messages.Count);
    }
    JsonResult retval = this.Json(response);
    Logger.Debug("Rendered response: {0}", retval.);
    return retval;
  }
  catch (Exception ex)
  {
    Logger.ErrorException("Failed rendering the response.", ex);
    return this.Json(null);
  }
}

しかし、実際には何も送信されません。

Fiddler を確認すると、リクエストが表示されますが、応答がありません。

[SessionState(SessionStateBehavior.ReadOnly)]  
public class ChatController : AsyncController

また、SessionStateBehaviour を Readonly に設定する必要がありました。そうしないと、非同期操作によってページ全体がブロックされてしまいます。

編集: CheckForMessagesAsync は次のとおりです。

public void CheckForMessagesAsync(Action<List<ChatMessage>> onMessages)
{
  if (onMessages == null)
    throw new ArgumentNullException("onMessages");

  Task task = Task.Factory.StartNew(state =>       
  {
    List<ChatMessage> msgs = new List<ChatMessage>();
    ManualResetEventSlim wait = new ManualResetEventSlim(false);
    Action<List<ChatMessage>> callback = state as Action<List<ChatMessage>>;
    if (callback != null)
    {
      IDisposable subscriber = m_messages.Subscribe(chatMessage =>
      {
        msgs.Add(chatMessage);
        wait.Set();
      });
      bool success;
      using (subscriber)
      {
        // Wait for the max seconds for a new msg
        success = wait.Wait(TimeSpan.FromSeconds(ChatServer.MaxWaitSeconds));              
      }
      if (success) this.SafeCallOnMessages(callback, msgs);
      else this.SafeCallOnMessages(callback, null);
    }        
  }, onMessages);
}
private void SafeCallOnMessages(Action<List<ChatMessage>> onMessages, List<ChatMessage> messages)
{
  if (onMessages != null)
  {
    if (messages == null)
      messages = new List<ChatMessage>();
    try
    {
      onMessages(messages);
    }
    catch (Exception ex)
    {
      this.Logger.ErrorException("Failed to call OnMessages callback.", ex);
    }
  }
}

参照されたブログ投稿と同じ考え方です

EDIT2:ところで、何も受信されていないため、待機タイムアウトが発生すると、応答が返されます。だからどこかで壊れそうです。これをログに記録する方法はありますか?

4

1 に答える 1

1

jQUERY リクエスト (元のブログ投稿を参照) を POST から GET に変更しました。それはそれを修正します。

于 2011-07-04T14:12:41.123 に答える