Asp.net server push technology of Server Push

  • 2020-05-07 19:30:59
  • OfStack

What if you want to maintain the connection between the server and the browser for a long time? Long connection technology, this is not a new technology, IFrame as a hidden frame to the long request page method has been used by many people on the Internet, but IFrame as a hidden frame has a drawback, that is, the browser's progress bar is always in the read state. To give users a better experience, the "genius of Google" solved this problem by using an object called htmlfile and applied it to both GMail and GTalk products.
Now the new project our company is going to do requires the real-time alarm function. I originally wanted to do AJAX polling, but I felt that I didn't pursue it. Some time ago, I heard about Server Push, but I didn't study it carefully. There's not a lot of data, and there are a lot of developers out there who think long connections are a myth, and there's even an HTTP protocol out there to prove it...
Without further discussion, let's introduce 1 long connection technology. The common long link is to make a web page, write a label IFrame in it, set the height and width to 0, SRC attribute points to a web page, such as ASPX, and then do nothing else in this file, just call Context.Response.Write method, output what? If the client has a method Change(time) with a change time, the output is (" < script > window.parent.Change("+DateTime.Now.ToString()+") < /script > "), which is a continuous output of client function calls in an endless loop, so that the browser and the server form a continuous data transmission link.
So what is htmlfile? This is an ActiveXObject similar to the Window object in Javascript. It also has an DOM structure inside. Write IFrame as a hidden frame into this object to solve the progress bar problem. This may be a bit more obscure, but look at the example code:
Default.aspx.cs
c # code
 
public partial class _Default : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
} 
protected override void Render(HtmlTextWriter output) 
{ 
string str; 
while (true) 
{// Dead loop keeps long links  
str = "<script >window.parent.Change('" + DateTime.Now.ToLongTimeString() + "')</script>"; 
this.Context.Response.Write(str); 
this.Context.Response.Flush();// Call out the script  
System.Threading.Thread.Sleep(1000); 
} 
} 
} 

WebForm1.aspx
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title>Asp.net Server Push</title> 
<script type="text/javascript"> 
function Change(str){ 
window.document.getElementById("div1").innerText=str; 
} 
function onload(){ 
var ifrpush = new ActiveXObject("htmlfile"); //  Create an object  
ifrpush.open(); // Open the  
var ifrDiv = ifrpush.createElement("div"); // add 1 a DIV 
ifrpush.appendChild(ifrDiv); // Added to the  htmlfile 
ifrpush.parentWindow.Change=Change; // registered  javascript  methods   Don't understand why register  
ifrDiv.innerHTML = "<iframe src='Default.aspx'></iframe>"; // in div add  iframe 
ifrpush.close(); // Shut down  
} 
onload(); 
</script> 
</head> 
<body> 
<div style=" float:left"> Now the time is: </div> 
<div id="div1"></div> 
</body> 
</html> 

Default.aspx does not need to be modified.
serverpush.rar

Related articles: