1. Create a comet_by_asp.net Web project2. Handler class and result classnamespacehandle{/// <summary> ///Summary description for Asnyhandler/// </summary> Public classAsnyhandler:ihttpasynchandler { PublicAsnyhandler () {} PublicIAsyncResult BeginProcessRequest (HttpContext context, AsyncCallback CB,Objectextradata) { //Myasynresult is a class that implements the IAsyncResult interface, when the callback function of CB is not called, the request will not be returned to the client, and will remain in the connected stateMyasynresult AsyncResult =NewMyasynresult (Context, CB, Extradata); String content= Context. request.params["content"]; //Add the message to the messages classmessages.instance (). AddMessage (content, AsyncResult); returnAsyncResult; } #regionDon't bother Public voidendprocessrequest (IAsyncResult result) {} Public BOOLisreusable {Get{return false; ; } } Public voidProcessRequest (HttpContext context) {}#endregion } Public classMyasynresult:iasyncresult {BOOL_iscompleted =false; PrivateHttpContext context; PrivateAsyncCallback CB; Private ObjectExtradata; PublicMyasynresult (HttpContext context, AsyncCallback CB,Objectextradata) { This. Context =context; This. cb =CB; This. Extradata =Extradata; } Private string_content; Public stringContent {Get{return_content;} Set{_content =value;} } #regionIAsyncResult interface Public Objectasyncstate {Get{return NULL; } } PublicSystem.Threading.WaitHandle AsyncWaitHandle {Get{return NULL; } } Public BOOLcompletedsynchronously {Get{return false; } } Public BOOLiscompleted {Get{return_iscompleted;} } #endregion //In the Add message method in the message class, this method is called to input the message to the client, thus enabling the broadcast function Public voidSend (Objectdata) {context. Response.Write ( This. Content); if(CB! =NULL) {CB ( This); } _iscompleted=true; ; } }}3. Messages ClassusingHandle;usingSystem;usingSystem.Collections.Generic;usingsystem.web;namespacehandle{/// <summary> ///Summary description for Messages/// </summary> Public classMessages {//log all requests to the clientlist<myasynresult> clients =NewList<myasynresult>(); #regionA single example that implements this classPrivate Static ReadOnlyMessages _instance =NewMessages (); PrivateMessages () {} Public StaticMessages Instance () {return_instance; } #endregion Public voidAddMessage (stringcontent, Myasynresult asyncResult) { //When the incoming content is "1", it is expressed as a connection request, that is, to maintain a connection from the client to the server//The connection is saved to list<myasynresult> clients, and when a message is sent, the connection will be traversed and the connection will be output to end the connection. if(Content = ="-1") {clients. ADD (AsyncResult); } Else { //outputs the contents of the current request to the clientAsyncresult.content =content; Asyncresult.send (NULL); //Otherwise, all cached clients are traversed and the current content is output to the client foreach(Myasynresult resultinchclients) {result. Content=content; Result. Send (NULL); } //Clear All cachesclients. Clear (); } } }}4. Default.aspx<%@ Page language="C #"%><! DOCTYPE HTML Public"-//W3C//DTD XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD">"http://www.w3.org/1999/xhtml">"Server"> <title></title> <style type="Text/css"> *{font-size:12px;} #divResult {border:1px solid # the; width:250px; } </style> <script type="Text/javascript"Src="Scripts/jquery-1.10.2.min.js"></script> <script type="Text/javascript"Src="Scripts/myjs.js"></script>Broadcast content:<input type="text"Id="content"/><br/>Message Logging:<div id="Divresult"></div> <input type="Button"Id="Btnsend"Value="Broadcast"/></body>5. Myjs class/// <reference path= "Jquery-1.3.2.min.js" >$ (document). Ready (function () {function send () {//send request to Comet_broadcast.asyn, message body is content in text box, request receive class is Asnyhandler$.post ("Comet_broadcast.asyn", {content: $ ("#content"). Val ()}); //Clear Content$("#content"). Val (""); } function Wait () {$.post ("Comet_broadcast.asyn", {content:"-1"}, function (data, status) {varresult = $ ("#divResult"); Result.html (result.html ()+"<br/>"+data); //the server returns the message and connects againwait (); }, "HTML" ); } //Initializing a connectionwait (); $("#btnSend"). Click (function () {send ();}); $("#content"). KeyPress (Function (Event) { if(Event. keycode = = -) {send (); } });});6. Web.configiis Classic Mode configuration<system.webServer> <modules runallmanagedmodulesforallrequests="true"> <remove name="FormsAuthentication"/> <remove name="applicationinsightswebtracking"/> <add name="applicationinsightswebtracking"Type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"precondition="Managedhandler"/> </modules> "Comet_broadcast"Path="Comet_broadcast.asyn"Type="Handle.asnyhandler"verb="Post,get"/> "false"/> </system.webServer>compatibility with older versions of IIS<system.web> "Comet_broadcast"Path="Comet_broadcast.asyn"Type="Handle.asnyhandler"verb="Post,get"/> 7Finally, I wish you good health and all the blessings.
ASP. NET Web for message push via IHttpAsyncHandler interface