web| Resolution | client | Data This article is Spanzhang original, its blog address is: Http://blog.csdn.net/spanzhang. Quote or paste, please specify the source, thank you!!
In order to show the data as the main task of the Web pages, such as stock-type Web pages, I hope to be able to real-time display data in the database. The popular solution is to make the data in XML format, the browser takes the XML data once in a while, and then updates it to the Web page. One is using Microsoft.XMLHTTP to get data from the server, and the other is to load the data on the server with Msxml2.domdocument. Both approaches are similar and are all XML routes to go. "Mencius E Chapter" On this pattern has written many articles, here is not discussed. My point here is to illustrate the incremental update process for the data.
Suppose our data is a two-dimensional relational data, and each row has a primary key that is uniquely labeled. The Web server starts a server clock (System.Timers.Timer) in the Application_Start, updating the data every once in a while. The XML data that the server provides to the client contains two files, one is all of the current version (All.aspx), and the other is the current version and the previous version of the incremental data (delta.aspx). The source code is as follows:
All.aspx file:
<%@ Page language= "C #"%>
<%
Response.Expires = 0;
Response.ContentType = "Text/xml";
Response.Charset = "UTF-8";
%>
<%
System.Data.DataTable dt = Sm_web. Logic.dt_all;
%>
<r v= "<%=sm_web. Logic.dataversion%> "><%
if (dt!= null)
{
for (int i=0;i<dt. rows.count;i++)
{
%><p a= "<%=dt. Rows[i]["A"]%> "b=" <%=dt. rows[i]["B"]%> "c=" <%=dt. rows[i]["C"]%> "d=" <%=dt. rows[i]["D"]%> "/><%
}
}
%></r>
Among them, Sm_web. Logic.dt_all and Sm_web. The logic.dataversion are all static variables. Examples of the generated XML data are as follows:
<r v= "1052" >
<p a= "0" b= "1002" c= "98.48" d= "Hunan"/>
<p a= "0" b= "1003" c= "26.37" d= "Shanghai"/>
</r>
Delta.aspx file:
<%@ Page language= "C #"%>
<%
Response.Expires = 0;
Response.ContentType = "Text/xml";
Response.Charset = "UTF-8";
%><r v= "<%=sm_web. Logic.dataversion%> "><%=sm_web. Logic.changedxml%></r>
Among them, Sm_web. Logic.changedxml is a static variable. Examples of the generated XML data are as follows:
<r v= "1052" >
<p a= "1" b= "1002" d= "Guangzhou"/>
<p a= "1" b= "1003" c= "25.00"/>
</r>
Here, A is a tagged field that indicates the type of update made on one row of data: 0: none; 1: Modify; 2: delete; 3: increase. All XML uses attributes to represent data in order to reduce network traffic, because A= "" is 3 characters less than <a></a>.
Each step of the server clock will be handled as follows:
1. Get all the current data;
2, compared with the previous data, assemble the change data sm_web. Logic.changedxml;
3, update sm_web. Logic.dt_all is the current data;
4, Sm_web. Logic.dataversion increased by 1.
This is how the entire server-side process is done. And on the client side, the page downloads the all.aspx content at the first load and initializes all the page elements (including a client data version number) and opens a clock that is less than half the server-side clock stepping speed (as for why, discussed later). There are several steps you need to take each time you step into the clock:
1, stop the clock step in;
2, asynchronous download delta.aspx;
When the delta.aspx download is complete, determine the version number of the new data:
1. If the current client data version number is the same, start the clock and return;
2, if the version number is larger than the current client data version number +1 (version number Break), then all.aspx load, update the data after the start of the clock;
3, otherwise to do the incremental data processing, update page local elements.
The above operation is placed inside a try block, and the client's clock is restarted when finished. For the server-side clock update speed and the client's clock update speed, need to be very careful to set. Because the client needs to reload all the data in the case of a broken version number, it is generally not necessary to set the client's time too long, at least less than half the server-side clock (consider network latency). But it cannot be too small for the client to lose the time to interact with the user. In one of my applications, the server-side clock update speed of 4 seconds, the client update speed of 1 seconds, you can guarantee that there is no version number break.
At this point, complete the technical framework, if you need more details, please send comments or mail to me.