基於ajax與msmq技術的訊息推送功能如何??

來源:互聯網
上載者:User

標籤:

本文和大家分享的主要是基於ajax與msmq技術的訊息推送功能相關實現方法,一起來看看吧,希望對大家學習ajax 有所協助。

我設計的這個推送demo是基於ajax長輪詢+msmq訊息佇列來實現的,具體互動過程如:

先說說這個ajax長輪詢,多長時間才算長呢?這個還真不好界定。

這裡是相對普通ajax請求來說的,通常處理一個請求也就是毫秒層級的時間。但是這裡的長輪詢方式

在ajax發送請求給伺服器之後,伺服器給調用端返回資料的時間多長那可還真不好說。嘿嘿,這關鍵要看

我們啥時候往msmq隊列中推送資料了,先看看推送的吧。。。。。


抱歉,沒弄張動態給大家。實現的功能大體上就是這樣。中的winform程式中我們點擊即刻發送按鈕,同時網頁上我們就能看到新推送的資料。

好了,說完具體實現流程和效果之後馬上就開始編碼實現吧。。。。

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 = "推送狀態";

}

}

}

訊息推送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服務端代碼

@{

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>

整頁模式代碼

當然,在這個只是一個初級的訊息推送demo,是否能勝任生產環境的需要還有待考證。

 

來源:部落格園

基於ajax與msmq技術的訊息推送功能如何??

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.