基于ajax與msmq技術(shù)的消息推送功能實現(xiàn)代碼
周末在家搗鼓了一下消息推送的簡單例子,其實也沒什么技術(shù)含量,歡迎大伙拍磚。
我設(shè)計的這個推送demo是基于ajax長輪詢+msmq消息隊列來實現(xiàn)的,具體交互過程如下圖:
先說說這個ajax長輪詢,多長時間才算長呢?這個還真不好界定。
這里是相對普通ajax請求來說的,通常處理一個請求也就是毫秒級別的時間。但是這里的長輪詢方式
在ajax發(fā)送請求給服務(wù)器之后,服務(wù)器給調(diào)用端返回數(shù)據(jù)的時間多長那可還真不好說。嘿嘿,這關(guān)鍵要看
我們啥時候往msmq隊列中推送數(shù)據(jù)了,先看看推送的效果圖吧。。。。。
抱歉,沒弄張動態(tài)效果圖給大家。實現(xiàn)的功能大體上就是這樣。上圖中的winform程序中我們點擊即刻發(fā)送按鈕,同時網(wǎng)頁上我們就能看到新推送的數(shù)據(jù)。
好了,說完具體實現(xiàn)流程和效果之后馬上就開始編碼實現(xiàn)吧。。。。
消息推送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 = "推送狀態(tài)"; } } }
Web服務(wù)端代碼
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 Task<ActionResult> GetMessage() { 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(); } } }
頁面視圖代碼
@{ ViewBag.Title = "Push"; } <h2>Push</h2> <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 + "<li>" + data + "</li>"); getMessage(); }); } </script>
當然,在這個只是一個初級的消息推送demo,是否能勝任生產(chǎn)環(huán)境的需要還有待考證。
如果你也有更好的實現(xiàn)和建議,都歡迎留言給我。
源碼在這里:http://pan.baidu.com/s/1hsHSLTy
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
typescript使用 ?. ?? ??= 運算符的方法步驟
本文主要介紹了typescript使用 ?. ?? ??= 運算符的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01JavaScript中三種非破壞性處理數(shù)組的方法比較
在這篇文章中,我們將會探索處理數(shù)組的三種方法:for…of循環(huán)、數(shù)組方法.reduce()和數(shù)組方法.flatMap(),文中的示例代碼講解詳細,感興趣的可以了解一下2023-06-06js利用正則表達式檢驗輸入內(nèi)容是否為網(wǎng)址
這篇文章主要為大家詳細介紹了js利用正則表達式檢驗輸入內(nèi)容是否為網(wǎng)址的相關(guān)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-07-07