标签:
需求:
每当数据库有数据更新时,推送到客户端
软需求:
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>
整个项目就是这样的,已通,有好的建议和问题欢迎留言!
利用Ajax+MSMQ(消息队列)+WebService实现服务器端向客户端的信息推送
标签:
原文地址:http://www.cnblogs.com/DByang/p/5654150.html