ASP.NET MVC4異步聊天室的示例代碼
本文介紹了ASP.NET MVC4異步聊天室的示例代碼,分享給大家,具體如下:
類圖:
Domain層
IChatRoom.cs
using System; using System.Collections.Generic; namespace MvcAsyncChat.Domain { public interface IChatRoom { void AddMessage(string message); void AddParticipant(string name); void GetMessages( DateTime since, Action<IEnumerable<string>, DateTime> callback); void RemoveParticipant(string name); } }
IMessageRepo.cs
using System; using System.Collections.Generic; namespace MvcAsyncChat.Domain { public interface IMessageRepo { DateTime Add(string message); IEnumerable<string> GetSince(DateTime since); } }
ICallbackQueue.cs
using System; using System.Collections.Generic; namespace MvcAsyncChat.Domain { public interface ICallbackQueue { void Enqueue(Action<IEnumerable<string>, DateTime> callback); IEnumerable<Action<IEnumerable<string>, DateTime>> DequeueAll(); IEnumerable<Action<IEnumerable<string>, DateTime>> DequeueExpired(DateTime expiry); } }
ChatRoom.cs
using System; using System.Collections.Generic; using System.Linq; using System.Threading; using MvcAsyncChat.Svcs; namespace MvcAsyncChat.Domain { public class ChatRoom : IChatRoom { readonly ICallbackQueue callbackQueue; readonly IDateTimeSvc dateTimeSvc; readonly IMessageRepo messageRepo; public ChatRoom( ICallbackQueue callbackQueue, IDateTimeSvc dateTimeSvc, IMessageRepo messageRepo) { this.callbackQueue = callbackQueue; this.dateTimeSvc = dateTimeSvc; this.messageRepo = messageRepo; } public void AddMessage(string message) { var timestamp = messageRepo.Add(message); foreach (var callback in callbackQueue.DequeueAll()) callback(new[] { message }, timestamp); } public void AddParticipant(string name) { AddMessage(string.Format("{0} 已進(jìn)入房間.", name)); } public void GetMessages( DateTime since, Action<IEnumerable<string>, DateTime> callback) { var messages = messageRepo.GetSince(since); if (messages.Count() > 0) callback(messages, since); else callbackQueue.Enqueue(callback); } public void RemoveParticipant(string name) { AddMessage(string.Format("{0} left the room.", name)); } } }
InMemMessageRepo.cs
using System; using System.Collections.Generic; using System.Linq; namespace MvcAsyncChat.Domain { public class InMemMessageRepo : IMessageRepo { public InMemMessageRepo() { Messages = new List<Tuple<string, DateTime>>(); } public IList<Tuple<string, DateTime>> Messages { get; private set; } public DateTime Add(string message) { var timestamp = DateTime.UtcNow; Messages.Add(new Tuple<string, DateTime>(message, timestamp)); return timestamp; } public IEnumerable<string> GetSince(DateTime since) { return Messages .Where(x => x.Item2 > since) .Select(x => x.Item1); } } }
CallbackQueue.cs
using System; using System.Collections.Generic; using System.Linq; namespace MvcAsyncChat.Domain { public class CallbackQueue : ICallbackQueue { public CallbackQueue() { Callbacks = new Queue<Tuple<Action<IEnumerable<string>, DateTime>, DateTime>>(); } public Queue<Tuple<Action<IEnumerable<string>, DateTime>, DateTime>> Callbacks { get; private set; } public void Enqueue(Action<IEnumerable<string>, DateTime> callback) { Callbacks.Enqueue(new Tuple<Action<IEnumerable<string>, DateTime>, DateTime>(callback, DateTime.UtcNow)); } public IEnumerable<Action<IEnumerable<string>, DateTime>> DequeueAll() { while (Callbacks.Count > 0) yield return Callbacks.Dequeue().Item1; } public IEnumerable<Action<IEnumerable<string>, DateTime>> DequeueExpired(DateTime expiry) { if (Callbacks.Count == 0) yield break; var oldest = Callbacks.Peek(); while (Callbacks.Count > 0 && oldest.Item2 <= expiry) { yield return Callbacks.Dequeue().Item1; if (Callbacks.Count > 0) oldest = Callbacks.Peek(); } } } }
RequestModels文件夾實(shí)體類
EnterRequest.cs
using System; using System.ComponentModel; using System.ComponentModel.DataAnnotations; namespace MvcAsyncChat.RequestModels { public class EnterRequest { [DisplayName("名稱")] [Required, StringLength(16), RegularExpression(@"^[A-Za-z0-9_\ -]+$", ErrorMessage="A name must be alpha-numeric.")] public string Name { get; set; } } }
GetMessagesRequest.cs
using System; namespace MvcAsyncChat.RequestModels { public class GetMessagesRequest { public string since { get; set; } } }
SayRequest.cs
using System; using System.ComponentModel; using System.ComponentModel.DataAnnotations; namespace MvcAsyncChat.RequestModels { public class SayRequest { [Required, StringLength(1024), DataType(DataType.MultilineText)] public string Text { get; set; } } }
ResponseModels文件夾實(shí)體類
GetMessagesResponse.cs
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 class='" + (type || "") + "'>" + 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
@model MvcAsyncChat.RequestModels.EnterRequest @{ View.Title = "Enter"; Layout = "~/Views/Shared/_Layout.cshtml"; } @section Head {} <tr id="enterSection"> <td> <h2>[MVC聊天]是使用ASP.NET MVC 3的異步聊天室 <table> <tr> <td class="form-container"> <fieldset> <legend>進(jìn)入聊天室</legend> @using(Html.BeginForm()) { @Html.EditorForModel() <input type="submit" value="Enter" /> } </fieldset> </td> </tr> </table> </td> </tr> @section PostScript { <script> $(document).ready(function() { $("#Name").focus(); }); </script> }
Room.cshtml
@using MvcAsyncChat; @using MvcAsyncChat.RequestModels; @model SayRequest @{ View.Title = "Room"; Layout = "~/Views/Shared/_Layout.cshtml"; } @section Head { <script src="@Url.Content("~/Scripts/room.js")"></script> } <tr id="messagesSection"> <td></td> </tr> <tr id="actionsSection"> <td> <label for="actionsList">操作:</label> <ul id="actionsList"> <li>@Html.RouteLink("離開房間", RouteName.Leave)</li> </ul> @using (Ajax.BeginForm("say", new { }, new AjaxOptions() { OnFailure = "onSayFailed", OnSuccess = "onSay", HttpMethod = "POST", }, new { id = "sayForm"})) { @Html.EditorForModel() } </td> </tr> @section PostScript { <script> $(document).ready(function() { $("#Text").attr("placeholder", "你說:"); $("#Text").focus(); setSayHandler(); getMessages(); }); </script> }
運(yùn)行結(jié)果如圖:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- asp.net mvc signalr簡單聊天室制作過程分析
- Asp.net使用SignalR實(shí)現(xiàn)聊天室的功能
- ASP.NET網(wǎng)站聊天室的設(shè)計(jì)與實(shí)現(xiàn)(第3節(jié))
- ASP.NET 使用application與session對象寫的簡單聊天室程序
- C#簡單聊天室雛形
- C#使用WebSocket實(shí)現(xiàn)聊天室功能
- C#實(shí)現(xiàn)簡易多人聊天室
- C#使用Socket實(shí)現(xiàn)本地多人聊天室
- C#制作簡單的多人在線即時(shí)交流聊天室
- ASP.net(C#)實(shí)現(xiàn)簡易聊天室功能
相關(guān)文章
Entity Framework Core延遲加載(懶加載)用法
這篇文章介紹了Entity Framework Core延遲加載(懶加載)的使用方式,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02為自己的ASP網(wǎng)站系統(tǒng)構(gòu)建一套標(biāo)記語言
為自己的ASP網(wǎng)站系統(tǒng)構(gòu)建一套標(biāo)記語言...2006-09-09MVC5 + EF6 + Bootstrap3 (11) 實(shí)現(xiàn)排序、搜索、分頁
本篇文章主要介紹了MVC5 + EF6 + Bootstrap3 (11) 實(shí)現(xiàn)排序、搜索、分頁,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考2016-12-12asp.net 繼承自Page實(shí)現(xiàn)統(tǒng)一頁面驗(yàn)證與錯誤處理
一直以來,我都在思考以前一個項(xiàng)目中,后臺文件中很多的.aspx文件上的權(quán)限判斷問題,傻乎乎的我基本上每個文件當(dāng)時(shí)都給加了一句2009-04-04