This article and we share the main is based on the Ajax and MSMQ technology of the message push function related implementation method, come together to see it, hope to learn Ajax help you.
I designed this push demo to be based on the Ajax long polling +MSMQ message queue to implement, the specific interaction process such as:
Let's talk about this Ajax long polling, how long is it? This is a really bad definition.
This is a relatively ordinary Ajax request, typically dealing with a request that is the millisecond level of time. But here's the long polling way
After the Ajax sends the request to the server, the server returns the data to the caller for a long time that is not very good to say. Hey, this is the key to see
When we push data into the MSMQ queue, let's look at the push ...
Sorry, I didn't get a dynamic for everyone. This is largely the function of implementation. In the WinForm program we click the Send button immediately, and we can see the new push data on the page.
Well, after the concrete implementation of the process and the effect of the code immediately after the implementation of it ....
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 = "Push message cannot be empty"; return; }
Queue. Send (this. TextBox1.Text, "Messagepush");
this. Label1. Text = "message push succeeded";
}
Catch (Exception ex)
{
this. Label1. Text = string. Format ("Message push failed: {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 = "Push status";
}
}
}
Message Push WinForm Program code
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 Service-side code
@{
Viewbag.title = "Push";
}
<H2>Pushh2>
<div> Receive message list 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>
Page view Code
Of course, in this is only a primary message to push the demo, whether the need to be competent production environment is still to be researched.
Source: Blog Park
How does the message push function be implemented based on Ajax and MSMQ technology?