Today, I was wondering how the content on the server changes and how the client changes.
Generally, there are two methods. One is that the client uses js to asynchronously and periodically poll the server. This method is adopted by most people, but I was wondering if I could take the initiative to push it to the client only when there was a change on the server side? This method is not feasible for B/S, because B/S is connectionless and the request/response method cannot be kept. I searched and saw a friend in the blog garden write an article.ArticleThis is about server push. Let's see it here.CodeThe complete code is as follows:
Front-end:
1 <% @ Page Language = " C # " Autoeventwireup = " True " Codebehind = " Serverpushdemo. aspx. CS " Inherits = " Webdemo. serverpush. serverpushdemo " %>
2
3 <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
4
5 < Html Xmlns = "Http://www.w3.org/1999/xhtml" >
6 < Head Runat = "Server" >
7 < Title > No title page </ Title >
8 < Script Type = "Text/JavaScript" >
9 Function Setvalue (time)
10 {
11 Document. getelementbyid ( " Tbtime " ). Value = Time;
12 }
13 </ Script >
14 </ Head >
15 < Body >
16 < Form ID = "Form1" Runat = "Server" >
17 < Div >
18 < Input Type = "Text" ID = "Tbtime" />
19 </ Div >
20 </ Form >
21 </ Body >
22 </ Html >
23
Background code:
Code
Using System;
Using System. collections;
Using System. configuration;
Using System. Data;
Using System. LINQ;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. htmlcontrols;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. xml. LINQ;
Using System. Threading;
NamespaceWebdemo. serverpush
{
Public Partial ClassServerpushdemo: system. Web. UI. Page
{
Protected VoidPage_load (ObjectSender, eventargs E)
{
}
Protected Override Void Render (htmltextwriter writer)
{
Base . Render (writer );
Response. Buffer = True ;
Bool Isoutput = False ;
Response. Write ( "" );
Response. Flush ();
Int Lastsecond = 0 ;
While (Response. isclientconnected)
{
Thread. Sleep ( 300 );
If (Datetime. Now. Second ! = Lastsecond && ! Isoutput)
{
Lastsecond = Datetime. Now. Second;
Response. Write ( " <SCRIPT> setvalue (' " + Datetime. Now. tostring () + " ') \ N </SCRIPT> " );
Response. Flush ();
Isoutput = True ;
}
Else
{
Isoutput = False ;
}
}
}
}
}
The implementation principle of this method is that in the onrender event, the connection is suspended cyclically, because the server side has not completed the response, so it has been connected to the client, although this method can push data from the server to the client, the cost is too high, because the advantage of web is that there is no connection. In this way, each client occupies an IIS connection on the server. If there are more than users, it will be terrible. Therefore, I think this method is not desirable. In comparison, it is better to use the Ajax Method for regular round robin.
In the real sense, server push seems like I have found some articles that can be implemented in CGI. Click here to see. But I don't know how to implement it in. net. Please give me some advice.
I also found a comet framework for. Net pushing technology that can implement persistent connections. For details, click here. I will release the demo in another day.