99热99这里只有精品6国产,亚洲中文字幕在线天天更新,在线观看亚洲精品国产福利片 ,久久久久综合网

歡迎加入QQ討論群258996829
麥子學(xué)院 頭像
蘋果6袋
6
麥子學(xué)院

基于ajax與msmq技術(shù)的消息推送功能實(shí)現(xiàn)

發(fā)布時(shí)間:2017-01-07 14:53  回復(fù):0  查看:2317   最后回復(fù):2017-01-07 14:53  

本文和大家分享的主要是基于ajaxmsmq技術(shù)消息推送功能相關(guān)實(shí)現(xiàn)方法,一起來(lái)看看吧,希望對(duì)大家學(xué)習(xí)ajax 有所幫助。

  我設(shè)計(jì)的這個(gè)推送demo是基于ajax長(zhǎng)輪詢+msmq消息隊(duì)列來(lái)實(shí)現(xiàn)的,具體交互過(guò)程如下圖:

基于ajax與msmq技術(shù)的消息推送功能實(shí)現(xiàn)

先說(shuō)說(shuō)這個(gè)ajax長(zhǎng)輪詢,多長(zhǎng)時(shí)間才算長(zhǎng)呢?這個(gè)還真不好界定。

  這里是相對(duì)普通ajax請(qǐng)求來(lái)說(shuō)的,通常處理一個(gè)請(qǐng)求也就是毫秒級(jí)別的時(shí)間。但是這里的長(zhǎng)輪詢方式

  在ajax發(fā)送請(qǐng)求給服務(wù)器之后,服務(wù)器給調(diào)用端返回?cái)?shù)據(jù)的時(shí)間多長(zhǎng)那可還真不好說(shuō)。嘿嘿,這關(guān)鍵要看

  我們啥時(shí)候往msmq隊(duì)列中推送數(shù)據(jù)了,先看看推送的效果圖吧。。。。。

基于ajax與msmq技術(shù)的消息推送功能實(shí)現(xiàn)


抱歉,沒(méi)弄張動(dòng)態(tài)效果圖給大家。實(shí)現(xiàn)的功能大體上就是這樣。上圖中的winform程序中我們點(diǎn)擊即刻發(fā)送按鈕,同時(shí)網(wǎng)頁(yè)上我們就能看到新推送的數(shù)據(jù)。

  好了,說(shuō)完具體實(shí)現(xiàn)流程和效果之后馬上就開(kāi)始編碼實(shí)現(xiàn)吧。。。。

  namespace SenderApp

  {

  public partial class Form1 : Form

  {

  private string queueName = @".\Private$\pushQueue";

  private MessageQueue pushQueue = null;

  public Form1()

  {

  InitializeComponent();

  }

  private void button1_Click(object sender, EventArgs e)

  {

  try

  {

  var queue = this.GetQueue();

  if (string.IsNullOrEmpty(this.textBox1.Text)) { this.label1.Text = "推送消息不能為空"; return; }

  queue.Send(this.textBox1.Text, "messagePush");

  this.label1.Text = "消息推送成功";

  }

  catch (Exception ex)

  {

  this.label1.Text = string.Format("消息推送失敗:{0}",ex.Message);

  }

  }

  private MessageQueue GetQueue()

  {

  if (this.pushQueue == null)

  {

  if (!MessageQueue.Exists(queueName))

  {

  this.pushQueue = MessageQueue.Create(queueName);

  }

  else

  {

  this.pushQueue = new MessageQueue(queueName);

  }

  }

  return this.pushQueue;

  }

  private void textBox1_MouseDown(object sender, MouseEventArgs e)

  {

  this.textBox1.Text = "";

  this.label1.Text = "推送狀態(tài)";

  }

  }

  }

  消息推送Winform程序代碼

  namespace MessagePushWeb.Controllers

  {

  public class HomeController : Controller

  {

  private static string queueName = @".\Private$\pushQueue";

  private static MessageQueue pushQueue = null;

  public ActionResult Index()

  {

  return View();

  }

  public async TaskGetMessage()

  {

  string msg = await Task.Run(() => {

  return this.ReadMessage();

  });

  return Content(msg);

  }

  private MessageQueue GetQueue()

  {

  if (pushQueue == null)

  {

  if (MessageQueue.Exists(queueName))

  {

  pushQueue = new MessageQueue(queueName);

  pushQueue.Formatter = new XmlMessageFormatter(new string[] { "System.String" });

  }

  }

  return pushQueue;

  }

  private string ReadMessage()

  {

  var queue = GetQueue();

  Message message = queue.Receive();

  return message.Body.ToString();

  }

  }

  }

  Web服務(wù)端代碼

  @{

  ViewBag.Title = "Push";

  }

  <h2>Pushh2>

  <div>接收消息列表div><div id="msg">div>

  <script type="text/javascript">

  $(function () {

  getMessage();

  });

  function getMessage() {

  $.get("/home/getmessage", {}, function (data) {

  var _msg = $("#msg").html();

  $("#msg").html(_msg + "

· 

" + data + "

· 

· 

");

· 

  getMessage();

  });

  }script>

  頁(yè)面視圖代碼

  當(dāng)然,在這個(gè)只是一個(gè)初級(jí)的消息推送demo,是否能勝任生產(chǎn)環(huán)境的需要還有待考證。

 

來(lái)源:博客園



您還未登錄,請(qǐng)先登錄

熱門帖子

最新帖子

?