public
Last active

ChatController.cs - An Async Chat Controller for MVC 3

  • Download Gist
ChatController.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
using System.Web.MVC
 
public class ChatController : AsyncController
{
[AsyncTimeout(ChatServer.MaxWaitSeconds * 1002)]
public void IndexAsync()
{
AsyncManager.OutstandingOperations.Increment();
 
ChatServer.CheckForMessagesAsync(msgs =>
{
AsyncManager.Parameters["response"] = new ChatResponse
{
messages = msgs
};
AsyncManager.OutstandingOperations.Decrement();
});
}
 
public ActionResult IndexCompleted(ChatResponse response)
{
return Json(response);
}
 
[HttpPost]
public ActionResult New(string user, string msg)
{
ChatServer.AddMessage(user, msg);
return Json(new
{
d = 1
});
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.