利用Ajax+MSMQ(消息隊列)+WebService實現服務器端向客戶端的信息推送


需求:

每當數據庫有數據更新時,推送到客戶端

軟需求:

1、服務器資源有限,要求資源占用盡可能小;

2、項目可控,不許調用第三方不可信不穩定的方法。

已有事例:

1、58到家采用的方法是TCP的長連接,對服務器壓力較大;

2、redis等提供了訂閱推送服務,開源,但是定制化對開發者其開發語言水平要求較高,筆者水平達不到

最終方案:

解釋:

①②頁面加載時第一次請求數據,返回數據,加載,調用ajax2

③頁面加載即發出請求,但是此時沒有數據,於是就block,等待其他組件insert msg

④收取新msg后,返回值給ajax2

⑤ajax2調用ajax1

⑥ajax1刷新數據

block后ajax2只在服務器占用一個線程,資源消耗很小。

===================下面是代碼=======================

1、文件結構

2、receive.cs//MSMQ的接收類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Messaging;

namespace WebApplication1
{
    public class receive
    {
        public String receives()
        {
            var queue = new MessageQueue(@".\private$\MsgQueue");
            queue.Formatter = new XmlMessageFormatter(
                new string[] { "System.String" }
                );
            Message m = queue.Receive();
            return m.Body.ToString();
        }
    }
}

2、send.cs//MSMQ的發送類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Messaging;
namespace WebApplication1
{
    public class send
    {
        public string create(String msg)
        {
            try
            {
                if (!MessageQueue.Exists(@".\private$\MsgQueue"))
                {
                    MessageQueue.Create(@".\private$\MsgQueue");
                }
                var queue = new MessageQueue(@".\private$\MsgQueue");

                queue.Send(msg, "Label");
                return "ok";
            }
            catch (Exception ex)
            {
                return "error";
            }
        }
    }
}

3、MQGiver.asmx//核心WebService

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using WebApplication1;

namespace 服務器接收mq
{
    /// <summary>
    /// MQGiver 的摘要說明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消對下行的注釋。
    // [System.Web.Script.Services.ScriptService]
    [Serializable]
    public class MQGiver : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        public void GiveMsg()
        {
            receive r = new receive();
            String msg = r.receives();
            String Json = "{\"msg\":\"" + msg + "\"}";
            string callback = HttpContext.Current.Request["jsoncallback"];
            HttpContext.Current.Response.Write(callback + "(" + Json + ")");
        }
        [WebMethod]
        public void GiveData()
        {
            String Json = "{\"msg\":\"ok\"}";
            string callback = HttpContext.Current.Request["jsoncallback"];
            HttpContext.Current.Response.Write(callback + "(" + Json + ")");

        }
    }
}

4、MSGadd.aspx//模擬向MSMQ添加數據

前台代碼
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MSGadd.aspx.cs" Inherits="服務器接收mq.MSGadd" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </form>
</body>
</html>

//================================================
后台代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication1;

namespace 服務器接收mq
{
    public partial class MSGadd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            send s = new send();
            Label1.Text = s.create(TextBox1.Text);
        }

    }
}

5、調用界面

<html>
<head>
    <!--自己注意jq包的引用-->
    <script src="jq.js"></script>
</head>
<body>
    <script type="text/javascript">
    function getdata(){
        $.ajax({
            url: "http://localhost:55843/MQGiver.asmx/GiveData?jsoncallback=?",
            dataType: "jsonp",
            success: OnSuccess,
        });
        function OnSuccess(json) {
            $("#la").text(json.msg)
            getmsg();
        }
    }
        
    function getmsg(){
    $.ajax({
            url: "http://localhost:55843/MQGiver.asmx/GiveMsg?jsoncallback=?",
            dataType: "jsonp",
            success: OnSuccess1,
            });
        function OnSuccess1(json1) {
            getdata();
        }
    }
        getdata();
    </script>
    <label id="la">
    </label>\
</body>
</html>

整個項目就是這樣的,已通,有好的建議和問題歡迎留言!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM