WebServiceと組み合わせて、メッセージをクライアントにプロアクティブにプッシュ

6221 ワード

  :        ,      ,       ,   

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.     
 

 

 



    
    <script src="../Scripts/jquery-1.10.2.min.js"/>
    <script src="../Scripts/myJQAjax.js"/>
    <script src="../Scripts/myJQDialog.js"/>
    <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>


    <form id="form1" runat="server">
        <scriptmanager runat="server">
            <services>
                <servicereference path="SiteNoticeServ.asmx"/>
            </services>
        </scriptmanager>
    <div>
    
    </div>
    </form>


 
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.     
 

 

 


<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title/>


    <form id="form1" runat="server">
    <div>
        <textbox runat="server" id="txt_Content"/>
        <button runat="server" id="btn_sender" text="  " onclick="btn_sender_Click"/>
    </div>
    </form>


 
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;
        }
 
 
    }
} 
</string></string></noticemodel></noticemodel></code></pre>      
  <br/> 
  <br/> 
 </div> 
</div>
                            </div>
                        </div>