Implementation of message push function based on Ajax and MSMQ technology

Source: Internet
Author: User
Tags msmq

At the weekend at home to make a brief example of the message push, in fact, there is no technical content, welcome everyone to shoot bricks.
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 ....

namespacesenderapp{ Public Partial classForm1:form {Private stringQueueName =@". \private$\pushqueue"; PrivateMessageQueue Pushqueue =NULL;  PublicForm1 () {InitializeComponent (); }        Private voidButton1_Click (Objectsender, EventArgs e) {            Try            {                varQueue = This.                Getqueue (); if(string. IsNullOrEmpty ( This. TextBox1.Text)) { This. Label1. Text ="The 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); }        }        PrivateMessageQueue Getqueue () {if( This. Pushqueue = =NULL)            {                if(!messagequeue.exists (QueueName)) {                     This. Pushqueue =messagequeue.create (queuename); }                Else                {                     This. Pushqueue =NewMessageQueue (queuename); }            }            return  This. Pushqueue; }        Private voidTextbox1_mousedown (Objectsender, MouseEventArgs e) {             This. TextBox1.Text ="";  This. Label1. Text ="Push Status"; }    }}
Message Push WinForm program code
namespacemessagepushweb.controllers{ Public classHomecontroller:controller {Private Static stringQueueName =@". \private$\pushqueue"; Private StaticMessageQueue Pushqueue =NULL;  PublicActionResult Index () {returnView (); }         Public AsyncTask<actionresult>GetMessage () {stringmsg =awaitTask.run (() = {                return  This.            Readmessage ();                        }); returnContent (msg); }        PrivateMessageQueue Getqueue () {if(Pushqueue = =NULL)            {                if(Messagequeue.exists (queuename)) {Pushqueue=NewMessageQueue (queuename); Pushqueue.formatter=NewXMLMessageFormatter (New string[] {"System.String" }); }            }            returnPushqueue; }        Private stringReadmessage () {varQueue =Getqueue (); Message Message=queue.            Receive (); returnmessage.        Body.tostring (); }    }}
Web service-side code
@{viewbag.title = "Push";}<H2>Push</H2><Div>Receiving a list of messages</Div><DivID= "MSG"></Div><Scripttype= "Text/javascript">    $(function() {getMessage ();    }); functionGetMessage () {$.get ("/home/getmessage", {}, function(data) {var_msg= $("#msg"). html (); $("#msg"). html (_msg+ "<li>" +Data+ "</li>");        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.

If you also have better implementation and suggestions, please leave a message to me.

SOURCE is here: http://pan.baidu.com/s/1pKOPHY7

Implementation of message push function based on Ajax and MSMQ technology

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.