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

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

1.消息实体


    public class NoticeModel {
        public string Sender { getset; }
        public string Reciever { getset; }
        public string Content { getset; }
        public DateTime SendDateTime { getset; }
    } 
 
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<stringNoticeQueen> noticeQueens = new Dictionary<stringNoticeQueen>();
 
        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 { getset; }
        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;
        }
 
 
    }
} 
 



转载于:https://www.cnblogs.com/kangwl/p/171c87395cfe07f36621c052401b6d52.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值