using System; using System.Collections.Generic; namespace MvcAsyncChat.ResponseModels { public class GetMessagesResponse { public string error { get; set; } public IEnumerable<string> messages { get; set; } public string since { get; set; } } }
SayResponse.cs
using System; namespace MvcAsyncChat.ResponseModels { public class SayResponse { public string error { get; set; } } }
ChatController.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Async; using MvcAsyncChat.Domain; using MvcAsyncChat.RequestModels; using MvcAsyncChat.ResponseModels; using MvcAsyncChat.Svcs; namespace MvcAsyncChat.Controllers { public class ChatController : AsyncController { readonly IAuthSvc authSvc; readonly IChatRoom chatRoom; readonly IDateTimeSvc dateTimeSvc; public ChatController( IAuthSvc authSvc, IChatRoom chatRoom, IDateTimeSvc dateTimeSvc) { this.authSvc = authSvc; this.chatRoom = chatRoom; this.dateTimeSvc = dateTimeSvc; } [ActionName("enter"), HttpGet] public ActionResult ShowEnterForm() { if (User.Identity.IsAuthenticated) return RedirectToRoute(RouteName.Room); return View(); } [ActionName("enter"), HttpPost] public ActionResult EnterRoom(EnterRequest enterRequest) { if (!ModelState.IsValid) return View(enterRequest); authSvc.Authenticate(enterRequest.Name); chatRoom.AddParticipant(enterRequest.Name); return RedirectToRoute(RouteName.Room); } [ActionName("room"), HttpGet, Authorize] public ActionResult ShowRoom() { return View(); } [ActionName("leave"), HttpGet, Authorize] public ActionResult LeaveRoom() { authSvc.Unauthenticate(); chatRoom.RemoveParticipant(User.Identity.Name); return RedirectToRoute(RouteName.Enter); } [HttpPost, Authorize] public ActionResult Say(SayRequest sayRequest) { if (!ModelState.IsValid) return Json(new SayResponse() { error = "该请求无效." }); chatRoom.AddMessage(User.Identity.Name+" 说:"+sayRequest.Text); return Json(new SayResponse()); } [ActionName("messages"), HttpPost, Authorize] public void GetMessagesAsync(GetMessagesRequest getMessagesRequest) { AsyncManager.OutstandingOperations.Increment(); if (!ModelState.IsValid) { AsyncManager.Parameters["error"] = "The messages request was invalid."; AsyncManager.Parameters["since"] = null; AsyncManager.Parameters["messages"] = null; AsyncManager.OutstandingOperations.Decrement(); return; } var since = dateTimeSvc.GetCurrentDateTimeAsUtc(); if (!string.IsNullOrEmpty(getMessagesRequest.since)) since = DateTime.Parse(getMessagesRequest.since).ToUniversalTime(); chatRoom.GetMessages(since, (newMessages, timestamp) => { AsyncManager.Parameters["error"] = null; AsyncManager.Parameters["since"] = timestamp; AsyncManager.Parameters["messages"] = newMessages; AsyncManager.OutstandingOperations.Decrement(); }); } public ActionResult GetMessagesCompleted( string error, DateTime? since, IEnumerable<string> messages) { if (!string.IsNullOrWhiteSpace(error)) return Json(new GetMessagesResponse() { error = error }); var data = new GetMessagesResponse(); data.since = since.Value.ToString("o"); data.messages = messages; return Json(data); } } }
room.js
var since = "", errorCount = 0, MAX_ERRORS = 6; function addMessage(message, type) { $("#messagesSection > td").append("<div>" + message + "</div>") } function showError(error) { addMessage(error.toString(), "error"); } function onSayFailed(XMLHttpRequest, textStatus, errorThrown) { showError("An unanticipated error occured during the say request: " + textStatus + "; " + errorThrown); } function onSay(data) { if (data.error) { showError("An error occurred while trying to say your message: " + data.error); return; } } function setSayHandler() { $("#Text").keypress(function (e) { if (e.keyCode == 13) { $("#sayForm").submit(); $("#Text").val(""); return false; } }); } function retryGetMessages() { if (++errorCount > MAX_ERRORS) { showError("There have been too many errors. Please leave the chat room and re-enter."); } else { setTimeout(function () { getMessages(); }, Math.pow(2, errorCount) * 1000); } } function onMessagesFailed(XMLHttpRequest, textStatus, errorThrown) { showError("An unanticipated error occured during the messages request: " + textStatus + "; " + errorThrown); retryGetMessages(); } function onMessages(data, textStatus, XMLHttpRequest) { if (data.error) { showError("An error occurred while trying to get messages: " + data.error); retryGetMessages(); return; } errorCount = 0; since = data.since; for (var n = 0; n < data.messages.length; n++) addMessage(data.messages[n]); setTimeout(function () { getMessages(); }, 0); } function getMessages() { $.ajax({ cache: false, type: "POST", dataType: "json", url: "/messages", data: { since: since }, error: onMessagesFailed, success: onMessages, timeout: 100000 }); }
Chat视图文件夹
Enter.cshtml