Home  >  Article  >  Backend Development  >  About the implementation method of .Net MVC long polling

About the implementation method of .Net MVC long polling

巴扎黑
巴扎黑Original
2018-05-23 16:22:131936browse

This article mainly introduces the relevant information of .Net MVC to implement long polling in detail. It has certain reference value. Interested friends can refer to it

What is long polling? polling?

Long polling is a way to implement "server push" technology, which can transmit changes on the server to the client in real time without the client frequently refreshing and sending requests.

Principle of long polling?

The client sends an Ajax request to the server. After the server receives the request, it maintains the connection and does not return a message. It does not return the response information and close the connection until the relevant processing is completed. After the client receives the response information, , perform relevant processing, and then send a new request to the server after the processing is completed.

What are the application scenarios of long polling?

Long polling is often used in scenarios such as Web instant messaging, monitoring, and real-time quotation systems that require server-side changes to be sent to the client in real time.

What are the advantages and disadvantages of long polling?

Advantages: Frequent requests will not be sent to the server when there is no message.

Disadvantages: The server consumes more resources to maintain the connection

Implementation:

Front-end code:

We call it again in the callback Function to start the next request after each request is closed.

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

Backend code:

Our background Controller needs to use asynchronous and inherit the AsyncController base class

public class DateTimeController : AsyncController
  {
    public void GetTimeAsync()
    {
      //计时器,5秒种触发一次Elapsed事件    
      System.Timers.Timer timer = new System.Timers.Timer(5000);
      //告诉.NET接下来将进行一个异步操作    
      AsyncManager.OutstandingOperations.Increment();
      //订阅计时器的Elapsed事件  
      timer.Elapsed += (sender, e) =>
      { //保存将要传递给GetTimeCompleted的参数        
        AsyncManager.Parameters["nowdate"] = e.SignalTime;
        //告诉ASP.NET异步操作已完成,进行GetTimeCompleted方法的调用        
        AsyncManager.OutstandingOperations.Decrement();
      };
      //启动计时器    
      timer.Start();
    }
    public ActionResult GetTimeCompleted(DateTime nowdate)
    {
      return Json(new { date = nowdate.ToString("HH:mm:ss") + " Welecom " }, JsonRequestBehavior.AllowGet);
    }
  }

The above is the detailed content of About the implementation method of .Net MVC long polling. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn