Home  >  Article  >  Backend Development  >  Summary of .Net MVC implementation of long polling examples

Summary of .Net MVC implementation of long polling examples

零下一度
零下一度Original
2017-06-17 10:30:032007browse

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 closes 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 Web instant messaging, monitoring, real-time quotation systems and other scenarios where server changes need 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: It consumes more resources to keep the server connected.

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, Inherit 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 Summary of .Net MVC implementation of long polling examples. 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