Usually, the chat room displays the latest server information through constant refresh. The so-called pull technology must constantly establish connections, refresh information, and disconnect. This is very inefficient and slow. (If the speed is increased, frequent establishment of disconnected connections and page refreshing will increase resource consumption between the server and client)
Server push technology
After a connection is established, the server actively pushes the latest information to the client.
You do not need to constantly establish a disconnected connection, and do not need to refresh the page. In contrast, pushing technology is faster and more efficient.
The original ASP cannot effectively implement the push technology. The key is that ASP (VBScript or JScript) does not have the sleep () function.
I wrote one, which has a good effect. Let's take a look.
The source code is as follows:
<% @ Language = "JavaScript" codePage = "936" enablesessionstate = false %>
<HTML>
<Head>
<Title> normal test </title>
<Script language = JavaScript runat = Server>
/*
* Function name: evlon. Sleep (MS)
* Author: evlon (a cattle) MSN: niukl@msn.com QQ: 273352165
* Function: Simulate sleep in C Language
* Purpose: it will be useful when used. The simplest case is in the server of the chat room,
* The application content is written to the client cyclically, but the CPU usage is too high.
* Adding this function will hardly occupy the CPU.
*
*/
Function evlon ()
{
This. XH = new activexobject ("msxml2.serverxmlhttp ");
This. lresolvetimeout = 0; // timeout for DNS name resolution
This. lconnecttimeout = 500; // timeout time for establishing a Winsock connection
This. lsendtimeout = 0; // timeout value for sending data
This. lreceivetimeout = 0; // timeout for receiving response
This. XH. settimeouts (this. lresolvetimeout, this. lconnecttimeout, this. lsendtimeout, this. lreceivetimeout );
This. urlport = "http: // 127.0.0.1: 1111 ";
}
Evlon. Prototype. Sleep = function (MS)
{
VaR beg = new date ();
VaR COUNT = math. Floor (MS/500 );
VaR lastms = MS % 500;
This. XH. settimeouts (this. lresolvetimeout, this. lconnecttimeout, this. lsendtimeout, this. lreceivetimeout );
For (VAR I = 0; I <count + 1; ++ I)
{
// Check whether the time has passed.
VaR now = new date ();
If (MS <now-Beg)
{
Break;
}
Else
{
If (I = count)
{
This. XH. settimeouts (this. lresolvetimeout, lastms, this. lsendtimeout, this. lreceivetimeout );
}
// Sleep
Try
{
This. XH. Open ("get", this. urlport, false, null, null );
This. XH. Send ();
}
Catch (e ){
}
}
}
}
</SCRIPT>
<Script language = JavaScript>
Function CS (c)
{
With (document. getelementbyid ("con "))
{
Innerhtml = C + innerhtml;
}
}
</SCRIPT>
</Head>
<Body>
<Div id = con> </div>
<Script language = JavaScript runat = Server>
// The following is the test Program To check the CPU usage within the specified time.
Function testsleep (timespan/* seconds */)
{
Try
{
VaR beg = new date ();
VaR evlon = new evlon ();
VaR ncount = 0;
While (response. isclientconnected ())
{
VaR end = new date ();
Response. Write ("<SC" + "ript> CS ('" + end + "<br>'); </SC" + "ript> \ n ");
Response. Flush ();
VaR span = end-beg;
If (span> timespan * 10000000)
{
Break;
}
Ncount ++;
Evlon. Sleep (200 );
}
VaR end = new date ();
VaR span = end-beg;
VaR MSG = 'total run' + ncount. tostring () + 'count \ n usage time' + span + 'millisecond ';
Response. Write (MSG );
}
Catch (E)
{
Response. Write (E. Message );
}
}
Testsleep (5 );
</SCRIPT>
======================================
The following figure shows the running effect:
wed Oct 26 21:41:46 UTC + 0800 2005
wed Oct 26 21:41:46 UTC + 0800 2005
wed Oct 26 21:41:46 UTC + 0800 2005
wed Oct 26 21:41:46 UTC + 0800 2005
wed Oct 26 21:41:46 UTC + 0800 2005
wed Oct 26 21:41:45 UTC + 0800 2005
wed Oct 26 21:41:45 UTC + 0800 2005
wed Oct 26 21:41:45 UTC + 0800 2005
wed Oct 26 21:41:45 UTC + 0800 2005
wed Oct 26 21:41:44 UTC + 0800 2005 0800
wed Oct 26 21:41:44 UTC + 2005
wed Oct 26 21:41:44 UTC + 0800 2005
wed Oct 26 21:41:44 UTC + 0800 2005 0800
wed Oct 26 21:41:44 UTC + 2005
wed Oct 26 21:41:43 UTC + 0800 2005
wed Oct 26 21:41:43 UTC + 0800 2005
wed Oct 26 21:41:43 UTC + 0800 2005
wed Oct 26 21:41:43 UTC + 0800
wed Oct 26 21:41:43 UTC + 0800 2005
wed Oct 26 21:41:42 UTC + 0800 2005
wed Oct 26 21:41:42 UTC + 0800 2005