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

前端之家收集整理的这篇文章主要介绍了结合 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<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;
        }
 
 
    }
}
阅读原文
原文链接:/webservice/832729.html

猜你在找的WebService相关文章