ASP. NET MVC Comet push

Source: Internet
Author: User

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 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)] 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

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.