服务器推技术
通常,聊天室通过不断刷新来显示服务器的最新信息,即所谓的拉技术,必须不停的建立连接,刷新信息,断开连接,这样效率很低,而且速度很慢.(如果加快速度,频繁的建立断开连接刷新页面都会增加服务器与客户端的资源消耗)
服务器推技术
推技术就是建立一个连接以后,由服务器主动的把最新信息源源不断的推送到客户端
不需要不停的建立断开连接,不需要刷新页面,相比之下,推技术效率更好速度最快
原来的ASP之不能有效的实现推技术,关键在于ASP(VBSCRIPT OR JScript)没有Sleep()函数
我拟写了一个,效果不错,大家看一下
下面为原码:
<%@ Language="Javascript" codepage ="936" EnableSessionState=False %>
<html>
<head>
<title>阿牛测试</title>
<script language=javascript runat=server>
/*
* 函数名: evlon.sleep(ms)
* 作者: evlon(阿牛) MSN:niukl@msn.com QQ:273352165
* 功能: 模拟 C 语言的 Sleep
* 用途: 用到时就有用了,最简单的情况就是在聊天室的服务端中,
* 通过循环把Application的内容写到客户端,但这样会CPU占用率过高
* 这时把这个函数加入,会几乎不占用CPU
*
*/
function Evlon()
{
this.xh = new ActiveXObject("Msxml2.ServerXMLHTTP");
this.lresolveTimeout = 0; // 解析DNS名字的超时时间
this.lconnectTimeout = 500; // 建立Winsock连接的超时时间
this.lsendTimeout = 0 ; // 发送数据的超时时间
this.lreceiveTimeout = 0 ; // 接收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)
{
//先看一下时间过了没有
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>
//下面是测试程序,在指定时间内观查CPU占用率
function testSleep(timeSpan /*秒*/)
{
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 = '共运行' + nCount.toString() + '次\n占用时间' + span + '毫秒';
Response.Write(msg);
}
catch(e)
{
Response.Write(e.message);
}
}
testSleep(5);
</script>
========================
下面是运行效果:
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
Wed Oct 26 21:41:44 UTC+0800 2005
Wed Oct 26 21:41:44 UTC+0800 2005
Wed Oct 26 21:41:44 UTC+0800 2005
Wed Oct 26 21:41:44 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 2005
Wed Oct 26 21:41:43 UTC+0800 2005
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