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 classCometcontroller:asynccontroller//Asynccontroller that need to inherit from async { /// <summary> ///asynchronous methods to handle client-initiated requests/// </summary> Public voidIndexasync () {AsyncManager.OutstandingOperations.Increment (); asyncmanager.parameters["Info"] ="What's wrong ?"; 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> PublicActionResult indexcompleted (stringinfo) { returnJson (info, jsonrequestbehavior.allowget); } }
Look for a page and request this asynchronous action via Ajax:
<HTML><Head> <title>Ajax Testing</title> <Scriptsrc= "/content/jquery-1.10.2.min.js"></Script> <Scripttype= "Text/javascript"> $(function() {Getcometserverpush (); }) functionGetcometserverpush () {$.ajax ({cache:false, URL:'/comet/index', Success:function(data) {$ ("#info"). HTML (data); Getcometserverpush (); } }); } </Script></Head><Body> <DivID= "Info"></Div></Body></HTML>
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 application
Application: 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)] Public classCometcontroller:asynccontroller//Asynccontroller that need to inherit from async { /// <summary> ///asynchronous methods to handle client-initiated requests/// </summary> Public voidIndexasync () {AsyncManager.OutstandingOperations.Increment (); FileSystemWatcher FSW=NewFileSystemWatcher (); FSW. Filter="123.txt";//Monitor only 123.txt filesFSW. Path = Server.MapPath (@"/");//Setting the monitoring pathFSW. EnableRaisingEvents =true;//Start Monitoring//FileSystemWatcher has a problem that is triggered several times, but is not related to this push example, so it does not resolveFSW. Changed + = (ObjectSource, FileSystemEventArgs e) = ={asyncmanager.parameters["Info"] = System.IO.File.ReadAllText (Server.MapPath (@"/123.txt"), System.Text.Encoding.Default);; 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> PublicActionResult indexcompleted (stringinfo) { returnJson (info, jsonrequestbehavior.allowget); } }
More flow force function, keep the reader free to play.
ASP. NET MVC Comet push