Comet (reverse Ajax) is mainly used to maintain the connection with the server through HTTP persistent connections, and implement server push and bidirectional communication.
Benefits: 1. Resource Saving and low latency compared with Ajax polling. 2. Compared with websocket, it is widely used.
1. Create an empty Asp.net MVC project first
Add a controllerCodeIt can also be used in Asp.net webform)
Public Class Cometcontroller: Controller
{
Public Actionresult test ()
{
Response. Buffer = False ;
While (True )
{
Response. Write (datetime. Now. tostring ( " Yyyy-mm-dd hh: mm: SS fff " ) + " | " );
Thread. Sleep ( 500 );
}
// Cannot run here
Return Content ( "" );
}
}
}
2. Create a controller and view to display HTML
View code
Public ClassHomecontroller: Controller
{
//
//Get:/home/
PublicActionresult index ()
{
ReturnView ();
}
}
View code is important
@{
Layout = NULL;
}
<! Doctype html >
< Html >
< Head >
< Title > Index </ Title >
< Script Type = "Text/JavaScript" SRC = "Http://code.jquery.com/jquery-1.7.1.min.js" > </ Script >
< Script Language = "JavaScript" >
VaR REQ = False ;
VaR Lastdelimiterposition = - 1 ;
$ (Document). Ready ( Function (){
Getdata ();
});
Function Getdata (){
Loadxmldoc ( " /Comet/test " );
}
// Create an xhr instance
Function Createrequest (){
If (Window. XMLHttpRequest && ! (Window. activexobject )){
Try {
REQ = New XMLHttpRequest ();
} Catch (E ){
REQ = False ;
} // Branch for IE/Windows ActiveX version
} Else If (Window. activexobject ){
Try {Req = New Activexobject ( " Msxml2.xmlhttp " );} Catch (E ){
Try {
REQ = New Activexobject ( " Microsoft. XMLHTTP " );
} Catch (E ){
REQ = False ;
}
}
}
}
// Initiate a request
Function Loadxmldoc (URL ){
Try {
If (Req ){
Req. Abort ();
REQ = False ;
}
Createrequest ();
If (Req ){
Req. onreadystatechange = Processreqchange;
Req. Open ( " Get " , URL, True );
Req. Send ( "" );
} Else {
Alert ( ' Unable to create request ' );
}
} Catch (E) {alert (E. Message );}
}
// Check status
Function Processreqchange (){
If (Req. readystate = 3 ){
Try {
Processinput (req. responsetext );
If (Req. responsetext. Length > 3000 ){
Lastdelimiterposition = - 1 ; Getdata ();
}
}
Catch (E ){
Alert (E. Message );
}
}
}
// Split string
Function Processinput (input ){
VaR Text = Input;
VaR Nextdelimiter = Text. indexof ( ' | ' , Lastdelimiterposition + 1 );
If (Nextdelimiter ! = - 1 ){
VaR Timestamp = Text. substring (nextdelimiter + 1 );
If (Timestamp. Length > 0 ){
Lastdelimiterposition = Nextdelimiter;
Processtime (timestamp );
}
}
}
// Event output or triggered
Function Processtime (time ){
Document. getelementbyid ( ' Div1 ' ). Innerhtml = Time;
}
</ Script >
</ Head >
< Body >
< Div >
< Div ID = "Div1" >
</ Div >
< Div ID = "Div2" >
</ Div >
</ Div >
</ Body >
</ Html >
3. The final result is:
A time is displayed on the page, updated every half second
Of course, after you get the content, you can do what you want... Update Dom or execute Js. (fortunately, the eval method is used ~~)
4. This example is just an implementation based on Asynchronous JavaScript,
In fact, you can also use the <IFRAME> and <SCRIPT> labels. In particular, the script tag can access and execute cross-domain JavaScript.
Http://en.wikipedia.org/wiki/Comet_ (programming)
Some libraries may be used here: ASP. NET comet Ajax Library (reverse Ajax-server push) http://pokein.codeplex.com/