. Net MVC implements long polling and. netmvc polling

Source: Internet
Author: User
Tags event timer

. Net MVC implements long polling and. netmvc polling

What is long polling?

Long polling is a method of implementing server push technology. It can send changes in the server to the client in real time without the need for the client to frequently refresh and send requests.

Long polling principle?

The client sends an Ajax request to the server. After receiving the request, the server keeps the connection and does not return a message until the relevant processing is complete. After the client receives the response information, after processing, you can send a new request to the server.

Long polling application scenarios?

Long polling is often used in scenarios where Web communication, monitoring, and instant quotation systems need to send server changes to the client in real time.

What are the advantages and disadvantages of long polling?

Advantage: when there is no message, requests are not sent frequently to the server.

Disadvantage: resources are consumed when the server maintains a connection.

Implementation:

Front-end code:

In the callback, we call the function again to start the next request after each request is closed.

<div id="container"></div><script type="text/javascript">  $(function () {    function longPolling() {      $.getJSON("/DateTime/GetTime", function (json) {        $("#container").append(json.date + "<br/>");        longPolling();      });    };    longPolling();  });</script>

Background code:

The backend Controller must be asynchronous and inherit the AsyncController base class.

Public class DateTimeController: AsyncController {public void GetTimeAsync () {// timer, which triggers an Elapsed Event System in five seconds. timers. timer timer = new System. timers. timer (5000); // tell. NET and then perform an asynchronous AsyncManager operation. outstandingOperations. increment (); // subscribe to the timer's Elapsed Event timer. elapsed + = (sender, e) =>{// Save the parameter AsyncManager to be passed to GetTimeCompleted. parameters ["nowdate"] = e. signalTime; // tell ASP. NET asynchronous operation has been completed, and the GetTimeCompleted method is called AsyncManager. outstandingOperations. decrement () ;}; // start the timer. start ();} public ActionResult GetTimeCompleted (DateTime nowdate) {return Json (new {date = nowdate. toString ("HH: mm: ss") + "Welecom"}, JsonRequestBehavior. allowGet );}}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.