结合 WebService 实现消息 主动推送到客户端

说明:不需要复杂的技术,不需要长轮循,还是老技术实现,上代码

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 queue = new Queue();
 
        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 noticeQueens = new Dictionary();
 
        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" %>
 

 



    
    
    
    
    


    
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" %>
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 { /// /// SiteNoticeServ 的摘要说明 /// [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; } } }
阅读原文

你可能感兴趣的:(asp.net,web)