First, Introduction
The principle of comet push, implemented in ASP. NET MVC, is simple.
Server-side: Receives the server sends the AJAX request, the server side does not return, but will hold, until has the thing to inform the client, only then returns this request.
Client: Requests an asynchronous action, and sends another immediately when it receives a return.
Disadvantage: An ASP. NET processing thread will be occupied for a long time. But compared to polling, it saves bandwidth.
Example:
Create a new controller as follows:
Comet Server Push controller (need to set noasynctimeout to prevent long-time request pending timeout error) [Noasynctimeout, sessionstate ( SESSIONSTATEBEHAVIOR.READONLY)] public class Cometcontroller:asynccontroller //need to inherit from asynchronous Asynccontroller { //<summary> ////Async method, processing client initiated request ///</summary> public void Indexasync () { AsyncManager.OutstandingOperations.Increment (); asyncmanager.parameters["info"] = "what's Up"; AsyncManager.OutstandingOperations.Decrement (); } <summary>////Send a response to the client when the asynchronous thread finishes///</summary>// <param name= "token" > Data encapsulation Object </param> //<returns></returns> public ActionResult indexcompleted (string info) { return Json (info, jsonrequestbehavior.allowget); } }
Look for a page and request this asynchronous action via Ajax:
The above example, if you have a breakpoint on the action, will not stop seeing the breakpoint in the loop. Indicates that the asynchronous client is constantly pushing. Of course, this example simply illustrates the principle of push.
Second, the applicationApplication: Monitor a TXT file on the server and, when there is a change, push the content to the client.
Comet Server Push controller (need to set noasynctimeout to prevent long-time request pending timeout error) [Noasynctimeout, sessionstate (sessionstatebehavior.readonly)] P Ublic class Cometcontroller:asynccontroller//need to inherit from asynchronous Asynccontroller {//<summary>////Async method, Processing client initiated Requests///</summary> public void Indexasync () {Asyncmanager.outstandingoperati Ons. Increment (); FileSystemWatcher FSW = new FileSystemWatcher (); FSW. Filter = "123.txt"; Only monitor 123.txt file FSW. Path = Server.MapPath (@ "/"); Set the monitoring path FSW. EnableRaisingEvents = true; Startup monitoring//filesystemwatcher has a problem that triggers several times, but not with this push example, so FSW is not resolved. Changed + = (object source, FileSystemEventArgs e) + = {asyncmanager.parameters["info"] = Syst Em. Io. File.readalltext (Server.MapPath (@ "/123.txt"), System.Text.Encoding.Default); AsyncManager.OutstandingOperations.Decrement (); }; }//<summary&Gt Send a response to the client when the asynchronous thread finishes//</summary>//<param name= "token" > Data encapsulation Object </param>//< Returns></returns> public ActionResult indexcompleted (string info) {return Json (info, J Sonrequestbehavior.allowget); } }
More flow force function, keep the reader free to play.
ASP. NET MVC Comet push