說明:不需要復雜的技術,不需要長輪循,還是老技術實現,上代碼
1.消息實體
public class NoticeModel {
public string Sender { get; set; }
public string Reciever { get; set; }
public string Content { get; set; }
public DateTime SendDateTime { get; set; }
}
2. 消息隊列
public class NoticeQueen {
private ManualResetEvent resetEvent = new ManualResetEvent(false);
private Queue<NoticeModel> queue = new Queue<NoticeModel>();
public void EnQueen(NoticeModel noticeModel) {
lock (queue) {
queue.Enqueue(noticeModel);
resetEvent.Set();
}
}
public NoticeModel DeQueen() {
resetEvent.WaitOne();
lock (queue) {
if (this.queue.Count == 1) {
this.resetEvent.Reset();
}
return queue.Dequeue();
}
}
}
3.消息適配
public class NoticeAdapter {
public Dictionary<string, NoticeQueen> noticeQueens = new Dictionary<string, NoticeQueen>();
public static NoticeAdapter NoticeAdapterInstance = new NoticeAdapter();
public string AddClient(string clientName) {
try {
if (!this.noticeQueens.ContainsKey(clientName)) {
this.noticeQueens[clientName] = new NoticeQueen();
}
return clientName;
}
catch (Exception) {
return null;
}
}
public void AddNotice(NoticeModel noticeModel) {
if (noticeQueens.ContainsKey(noticeModel.Reciever)) {
NoticeAdapterInstance.noticeQueens[noticeModel.Reciever].EnQueen(noticeModel);
}
}
public NoticeModel GetNotice(NoticeModel noticeModel) {
if (noticeQueens.ContainsKey(noticeModel.Reciever)) {
return NoticeAdapterInstance.noticeQueens[noticeModel.Reciever].DeQueen();
}
return new NoticeModel();
}
}
4.消息接收頁
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="NoticeReciever.aspx.cs" Inherits="WebApplication.SiteNotice.NoticeReciever" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="../Scripts/jquery-1.10.2.min.js"></script>
<script src="../Scripts/myJQAjax.js"></script>
<script src="../Scripts/myJQDialog.js"></script>
<script>
var webServ;
$(function () {
webServ = WebApplication.SiteNotice.SiteNoticeServ;
GetNotice();
});
function GetNotice() {
webServ.WaiteNotice("<%=UserID%>",function (res) {
if (res.Content != null) {
callBack(res);
}
});
}
function callBack(res) {
myJQDialog.rightBottomAlert("webnotice", res.Content);
GetNotice();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server">
<Services>
<asp:ServiceReference Path="SiteNoticeServ.asmx"/>
</Services>
</asp:ScriptManager>
<div>
</div>
</form>
</body>
</
html
>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication.SiteNotice {
public partial class NoticeReciever : System.Web.UI.Page {
public string UserID { get; set; }
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
UserID = Session.SessionID;
NoticeAdapter.NoticeAdapterInstance.AddClient(Session.SessionID);
}
}
}
}
5.消息發送頁
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="NoticeSender.aspx.cs" Inherits="WebApplication.SiteNotice.NoticeSender" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" ID="txt_Content"></asp:TextBox>
<asp:Button runat="server" ID="btn_sender" Text="發送" OnClick="btn_sender_Click"/>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication.SiteNotice {
public partial class NoticeSender : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
}
protected void btn_sender_Click(object sender, EventArgs e) {
var clients = NoticeAdapter.NoticeAdapterInstance.noticeQueens;
foreach (var noticeQueen in clients) {
NoticeAdapter.NoticeAdapterInstance.AddNotice(new NoticeModel() {
Reciever = noticeQueen.Key,
Content = txt_Content.Text
});
}
}
}
}
6.webservice 調用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebApplication.SiteNotice {
/// <summary>
/// SiteNoticeServ 的摘要說明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消注釋以下行。
[System.Web.Script.Services.ScriptService]
public class SiteNoticeServ : System.Web.Services.WebService {
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public NoticeModel WaiteNotice(string reciever) {
var model = NoticeAdapter.NoticeAdapterInstance.GetNotice(new NoticeModel() { Reciever = reciever });
return model;
}
}
}
