发新话题
打印

XMLHTTP+Javascript+Asp写的聊天室,无刷新实现

XMLHTTP+Javascript+Asp写的聊天室,无刷新实现

使用XML.而无须不断刷新*.ASP.
无刷新实现发言的逐条显示.
使用Application变量存储聊天记录,所以要需要建立一个虚拟目录
服务器端不需要支持XML,只要支持ASP和能建立虚拟目录就可以了.
以下代码在nt4.0+iis4.0上通过.我估计在你的win2000下也差不多吧.
如果有问题,请多多赐教:netnice@chinaok.net


##########index.asp##############################

<html>
<head>
<title>蓝蝶聊天室 Www.ChinaOk.net</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style TYPE="text/css">
BODY{FONT-SIZE: 9pt}
TD{FONT-SIZE: 9pt}
.tbljm{BORDER-BOTTOM: #000000 1px solid; BORDER-LEFT: #000000 1px solid; BORDER-RIGHT: #000000 1px solid; BORDER-TOP: #000000 1px solid;color:#000000;}
</style>
<script language="Javascript">
function openWin()
{winst="Status=yes,scrollbars=yes,resizable=yes,width="+(screen.availWidth-10)+",height="+(screen.availHeight-50)+",top=0,left=0";
window.open('about:blank','7923sdfsdfs',winst);
}
function form_onsubmit(obj)
{ var no=0,psd="1234567890";
if(obj.UserName.value==""){alert("请输入你的昵称!\n"); obj.UserName.focus(); return false;}
if(obj.UserName.value.indexOf(" ")>-1 ||obj.UserName.value.indexOf("'")>-1 || obj.UserName.value.indexOf("\"")>-1)
{alert("你的昵称含有非法字符!\n"); obj.UserName.focus(); return false;}
openWin();
return true;
}
</script>
</head>

<body bgcolor="#ffffff" text="#000000" valign="center" onload="javascript:document.form1.ok.blur()"
background="images/greystrip.gif">
<center><TABLE border=0 width="67%">
<TBODY>
<TR>
<TD valign=center align="middle" style="FONT-SIZE: 10.5pt"><FONT color=#0033ff>::::: :::::</FONT> </TD>
</TR></TBODY></TABLE></center>
<table width="200" border="0" style="BORDER-RIGHT: #003366 1px solid; BORDER-TOP: #003366 1px solid; BORDER-LEFT:
#003366 1px solid; BORDER-BOTTOM: #003366 1px solid" cellspacing="0" cellpadding="0" align="center">
<tr>
<td bgcolor="#304d7c" height="25">
<div align="center"><font color="#ffffff">用户登陆</font></div>
</td>
</tr>
<tr>
<td height="121">
<form name="form1" method="post" action="main.asp" onSubmit="return form_onsubmit(this)" target="7923sdfsdfs">

<table width="199" border="0" cellspacing="0" cellpadding="0" background="images/greystrip.gif">
<tr>
<td height="35">
<div align="center">用户名:
<input name="UserName" size="10" maxlength="20" class=tbljm>
</div>
</td>
</tr>
<tr>
<td height="41">
<div align="center">性 别:
<input type=radio value="0" name=sx checked>男 | <input type="radio" value="1" name=sx>女
</div>
</td>
</tr>
<tr>
<td height="40">
<div align="center">
<input type="submit" name="ok" value="登 陆" class=tbljm>
<input type="reset" name="nook" value="重 写" class=tbljm><BR><br><font color=red>
演示xmlHttp实现的无刷新技术<br>代码可下载</font><br></div>

</td>
</tr>
</table>
</form></td>
</tr>
</table>
</body>
</html>


################main.asp######################

<%Response.Buffer=true
Response.Expires=0
userName=Request.Form("username")
if username="" then Response.Redirect "index.asp"
session("u_name")=username
if Request.Form("sx")="1" then
session("u_sx")="1"
else
session("u_sx")="0"
end if
%>
<html>

<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
<link REL="SHORTCUTICON" url="images/Bluenote.ico">
<title>蓝蝶聊天室::欢迎您! Www.ChinaOk.net .............蓝蝶精品网.............Www.ChinaOk.net</title>
<style>
.fbd{BORDER-BOTTOM-COLOR: #3A5C9F; BORDER-BOTTOM-STYLE: solid; BORDER-LEFT-COLOR: #3A5C9F; BORDER-LEFT-STYLE: solid;
BORDER-RIGHT-COLOR: #3A5C9F; BORDER-RIGHT-STYLE: solid; BORDER-TOP-COLOR: #3A5C9F; BORDER-TOP-STYLE: solid}
.topc{bgcolor:#ffff00}
</style>
<script Language="JavaScript">
self.onerror=null;
var nullframe = '<HTML><BODY BGCOLOR=#003366 text=#ffffff><center><H3 color=#ffffff>正在读取资料, 请稍候 ......</h3></center></BODY></HTML>';
</script>
</head>
<frameset cols="*,0" rows="*" class=topc>
<frameset rows="*,75" cols='*' framespacing="0" frameborder="1" border="0" >
<frameset rows="45%,*" cols='*' framespacing="1" border="1" name="content" frameborder='YES' class=topc>
<frame name="pub" alt=XMLHTTP+Javascript+Asp写得聊天室,无刷新实现 src="javascript:parent.nullframe" scrolling="auto" frameborder=1 >
<frame name="my" alt=XMLHTTP+Javascript+Asp写得聊天室,无刷新实现 src="javascript:parent.nullframe" scrolling="auto" frameborder=1>
</frameset>
<frame name="sd" scrolling="no" noresize alt=XMLHTTP+Javascript+Asp写得聊天室,无刷新实现 src="send.asp" >
</frameset>
<noframes>
<p>此网页使用了框架,但您的浏览器不支持框架。</p>

</body>
</noframes>
</frameset>


</html>


################reload.asp##############

<%'Response.ContentType="text/xml"
Response.Write "<?xml version=""1.0"" encoding=""GB2312""?>"
Response.Write "<Msg>"
dim c_info,c_Lid,c_Ip,u_Lid,NewLid,u_name,i
c_info=Application("c_info")
u_Lid=session("u_Lid")
newLid=u_Lid
u_name="netnice"
for i=c_info(0)+6 to 240 step 6
if (c_info(i)>u_Lid) and ((c_info(i+1)=u_name) or (c_info(i+2)=u_name) or (c_info(i+2)="大家") or (c_info(i+3)="0")) then
Response.Write c_info(i+5)
if (newLid<c_info(i)) then newLid=c_info(i)
end if
next
for i=1 to c_info(0) step 6
if (c_info(i)>u_Lid) and ((c_info(i+1)=u_name) or (c_info(i+2)=u_name) or (c_info(i+2)="大家") or (c_info(i+3)="0")) then
Response.Write c_info(i+5)
if (newLid<c_info(i)) then newLid=c_info(i)
end if
next
Response.Write "</Msg>"
session("u_Lid")=newLid
%>

########say.asp##############

<% Option Explicit
dim F,T,sC,cL,E,W,sJ
Response.ContentType="text/xml"
F=Request.Form ("F")
T=Request.Form ("T")
sC=Request.Form ("sC")

cL=Request.Form ("cL")
E=Request.Form("E")
'W=Request.Form("W")
W="0"
'发言时间
sJ=getTime()
if Request.ServerVariables("REQUEST_METHOD")="POST" then
sC=""&trim(F)&""&trim(T)&""&E&""&trim(sC)&""
&trim(cL)&""&sJ&""
else
Response.End
end if
dim c_info,c_Lid,c_Ip,u_Lid,NewLid,u_name,i
Application.Lock
c_info=Application("c_info")
c_Lid=Application("c_Lid")+1
Application("c_Lid")=c_Lid
c_Ip=(c_info(0)+6) mod 240
c_info(0)=c_Ip
c_info(c_Ip)=c_Lid
c_info(c_Ip+1)=F
c_info(c_Ip+2)=T
c_info(c_Ip+3)=W
c_info(c_Ip+4)=""
c_info(c_Ip+5)=sC '""&trim(F)&""&trim(T)&""&E&""&trim(sC)&""
&trim(cL)&""&sJ&""
Application("c_info")=c_info
Application.UnLock
u_Lid=session("u_Lid")
newLid=u_Lid
u_name=session("u_name")
'&Application("c_Lid")&"%"&c_info(0)

Response.Write ""
for i=c_info(0)+6 to 240 step 6
if (c_info(i)>u_Lid) and ((c_info(i+1)=u_name) or (c_info(i+2)=u_name) or
(c_info(i+2)="大家") or (c_info(i+3)="0")) then
Response.Write c_info(i+5)
if (newLidu_Lid) and ((c_info(i+1)=u_name) or (c_info(i+2)=u_name) or
(c_info(i+2)="大家") or (c_info(i+3)="0")) then
Response.Write c_info(i+5)
if (newLid
Response.Write ""
session("u_Lid")=newLid
function getTime()
dim y,m,d,h,n,s
y=Year(date())
m=Month(date())
d=Day(date())
h=Hour(time())
n=Minute(time())
s=Second(time()) '当为是一位的 时候在前面加0
if len(m)=1 then m="0" & m
if len(d)=1 then d="0" & d
if len(h)=1 then h="0" & h
if len(n)=1 then n="0" & n
if len(s)=1 then s="0" & s
getTime=y&"/"&m&"/"&d &" " & h&":"& n&":"&s
end function%>
这是最重要的一个了。也很大。我的习惯不太好。没有加详细的注释,认真看吧。
#########send.asp##############






td{font-family: "宋体"; font-size: 9pt}
body { font-family: "宋体"; font-size: 9pt}
select { font-family: "宋体"; font-size: 9pt}
.p9{font-family:"宋体";font-size:9pt;line-height:130%; width:68px;}
A {cursor:hand;text-decoration:none; font-family: "宋体"; font-size: 9pt;}
input{font-size:9pt}
.an{background-color:FFFFFF;color:000000;border: 1 double}

发言

var tmp0="$F对$T说:|无表情|$F向$T微微一笑:|微笑|$F笑嘻嘻地对$T说:|嘻笑|$F冲$T傻乎乎的笑笑:|傻笑|$F对$T娇笑着:|娇笑|$F对着$T哈哈大笑:|大笑|$F看着$T,心头一阵狂喜:|狂喜|$F冲着$T狂笑不已:|狂笑|$F对$T奸笑着:|奸笑|$F咯咯一笑,很大方地对$T说:|大方|$F紧紧地握住$T的手:|握手|$F向$T挥了挥手:|挥手|$F耸耸肩,两手一摊:$T,|耸肩1|$F无奈地耸耸肩:$T,|耸肩2|$F摸摸$T的头,细声安慰:|安慰|$F笑呵呵向$T一拱手:|拱手|$F很有礼貌地对$T作了一揖:|作揖|$F深深地鞠了一躬:$T,|鞠躬|$F不好意思地对$T说:|歉意|$F瞪大了眼睛,很讶异地看着$T:|讶异|$F大吃一惊:$T,|惊讶|$F茫然地看着$T:|茫然|$F一脸的迷茫:$T,|迷茫|$F颇感迷惑:$T,|迷惑|$F感到很是奇特:$T,|奇特|$F对$T潇洒地一甩头:|潇洒|$F惋惜地看着$T:|惋惜|$F对$T皱了皱眉:|皱眉|$F很遗憾地对$T说:|遗憾|$F大包大揽地向$T承诺:|承诺|$F抓抓头皮,还是不解:$T,|不解|$F提高嗓门,大声地对$T说:|大声|"
var tmp1="$F不解地问$T:|询问|$F向$T吐了吐舌头:|吐舌|$F非常自信地对$T说:|自信|$F向上天祈祷着,$T,|祈祷|$F向$T噘了噘嘴:|噘嘴|$F嘟着嘴对$T说:|嘟嘴|$F平静地对$T说:|平静|$F高兴地对$T说:|高兴|$F在$T耳边悄悄地说:|耳语|$F眯着小眼睛对$T说:|眯眼|$F眼波晶莹明亮地看着$T:|眼亮|$F幸福地看着$T:|幸福|$F很满足地对$T说:|满足|$F轻轻牵着$T的手,小声地说:|牵手|$F轻轻推了$T一下:|轻推|$F粘粘糊糊地向$T撒娇:|撒娇|$F对$T做了个鬼脸:|鬼脸|$F神秘兮兮地对$T说:|神秘|$F脸上泛起了红晕,低着头小声地对$T说:|脸红|$F低着头,玩弄着自己的手指,羞得满脸通红:$T,|害羞|$F小猫猫般地依偎在$T的怀中:|依偎|$F拍了拍$T那脏兮兮的小脸蛋:|拍脸|$F向$T抛去一个媚眼:|媚眼|$F恶狠狠地冲$T瞪着眼睛:|瞪眼|$F柔声地对$T说:|柔声|$F情意绵绵地望着$T说:|深情|$F温柔地抱住$T说:|拥抱|$F亲了亲$T的脸:|亲亲|$F恋恋不舍地拉着$T的衣角:|不舍|"
var tmp2="$F运足气一声断喝:$T!|断喝|$F幸灾乐祸地对$T说:|幸灾|$F露出无可奈何的表情:$T,|无奈|$F使劲敲敲自己脑门,看着$T:|拍脑|$F顾作沉思状:$T,|沉思|$F一付无辜的样子:$T,|无辜1|$F感到非常无辜,$T:|无辜2|$F感觉很不舒服:$T,|不适|$F打个大哈欠:$T,|哈欠|$F口吐白沫,晕倒在地:$T,|白沫|$F感到无聊极了:$T,|无聊|$F看着$T,快要吐了:|想吐|$F一脸无赖泼皮样子:$T,|无赖|$F歪着看着$T,二球劲十足:|二球|$F双眼盯着$T,奴才嘴脸十足:|奴才|$F委屈地对$T说:|委屈|$F大感沮丧:$T,|沮丧|$F望着窗外细雨淅淅,不由得双眼朦胧:$T,|伤感|$F悲伤地对$T说:|悲伤|$F泪水在眼框里打着转:$T,|含泪|$F看着$T,快要哭了:|要哭|$F低低抽泣着:$T,|抽泣|$F一把将$T抱住,哭哭涕涕:|抱哭|$F想到伤心处,泪流如注:$T,|大哭|$F抱头放声大哭:$T,|痛哭|$F号淘大哭:$T,|大哭|$F对$T露出失望的表情:|失望|$F用眼角斜视$T,满脸俱是轻蔑不屑之色:|轻蔑|$F生气地对$T说:|生气|"
var tmp3="$F向$T提出强烈抗议:|抗议|$F对$T大声叫嚷:|叫嚷|$F对$T气愤地嚷道|气愤|$F怨毒地看着$T:|怨毒|$F一把甩掉$T的手,恶恨恨地说:|甩手|$F目光尽赤,冲$T怒不可恶地狂吼:|暴怒|$F打了$T老大一个耳括子:|耳光|$F不客气地猛揍$T一拳:|拳打|$F一脚踢在$T的屁股上:|脚踢|$F向$T点头称是:|点头称是|$F兴高采烈地对$T说:|兴高采烈|$F惊慌失措地对$T说:|惊慌失措|$F幸灾乐祸地对$T说:|幸灾乐祸|$F眉飞色舞地对$T说:|眉飞色舞|$F阴阳怪气地对$T说:|阴阳怪气|$F哭天抢地地对$T说:|哭天抢地|$F牢骚满腹地对$T说:|牢骚满腹|$F理屈词穷地对$T说:|理屈词穷|$F江郎才尽地对$T说:|江郎才尽|$F凶神恶煞地对$T说:|凶神恶煞|$F眉歪眼斜地对$T说:|眉歪眼斜|$F目光凛然地对$T说:|目光凛然|$F声嘶力竭地对$T说:|声嘶力竭|$F意乱情迷地对$T说:|意乱情迷|$F脸色铁青地对$T说:|脸色铁青|$F失魂落魄地对$T说:|失魂落魄|$F目光呆滞地对$T说:|目光呆滞|$F正气凛然地对$T说:|正气凛然|"
var tmp4="$F手足无措地对$T说:|手足无措|$F无精打采地对$T说:|无精打采|$F依依不舍地对$T说:|依依不舍|$F拳打脚踢地对$T说:|拳打脚踢|$F不怀好意地对$T说:|不怀好意|$F毛手毛脚地对$T说:|毛手毛脚|$F得意洋洋地对$T说:|得意洋洋|$F慨叹万千地对$T说:|慨叹万千"
var etmp=tmp0+tmp1+tmp2+tmp3+tmp4;
var eV=etmp.split("|")
function wh(fn){
fn.open();
fn.writeln("");
fn.writeln("对话区");
fn.writeln(".t{color:003366;font-size:9pt;}.pic{font-size:17pt;}body{font-family:\"宋体\";font-size:10.5pt;line-height:160%}A{text-decoration:none}A:Hover{text-decoration:underline}A:visited{color:blue}");
fn.writeln("var autoScrollOn=1;var scrollOnFunction;var scrollOffFunction;");
fn.writeln("function scrollit(){ if(!parent.sd.oT.as.checked){autoScrollOn=0;return true;}else{autoScrollOn=1;StartUp();return true;}}function scrollWindow(){if(autoScrollOn==1){this.scroll(0,65000);setTimeout('scrollWindow()',200);}}function scrollOn(){autoScrollOn=1;scrollWindow();}function scrollOff(){autoScrollOn=0;}function StartUp(){parent.onblur=scrollOnFunction;parent.onfocus=scrollOffFunction;scrollWindow();}");
fn.writeln("scrollOnFunction=new Function('scrollOn()');");
fn.writeln("scrollOffFunction=new Function('scrollOff()');");
fn.writeln("StartUp();");return;
}
function write(){
wh(parent.pub.document);
parent.pub.document.writeln("");
parent.pub.document.writeln("◆蓝蝶聊天室
◆中心大厅
轻轻敲醒沉睡的心灵,慢慢张开你的眼睛,让那昨日的忧伤随聊而去!");
wh(parent.my.document);
var myd=parent.my.document;
myd.writeln("");
myd.writeln("◆蓝蝶聊天室◆温馨包厢
");
myd.writeln("[这是您的个人包厢,只显示和您相关的发言!]");
}


Function URLEncoding(vstrIn)
strReturn = ""
For i = 1 To Len(vstrIn)
ThisChr = Mid(vStrIn,i,1)
If Abs(Asc(ThisChr))
Function bytes2BSTR(vIn)
strReturn = ""
For i = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn,i,1))
If ThisCharCode

var Me="";

var Msx="girl.gif";

var Msx="boy.gif";

var Ts=new oS();
function oS()
{this.F="";
this.T="";
this.E="";
this.W="0";
this.sC="";
this.cL="";
this.Send=sendMsg;
}
function checksays()
{if (document.say.says.value=="") {alert("你要说什么呢?");return false;}
var fm=document.say;
var sC=fm.says.value;
var F=Me
var T=fm.T.options[0].value;
var E=document.oT.userface.selectedIndex;
var W="0"
var cL=fm.says.style.color;
fm.says.value="";
fm.says.focus();
Ts.F=F;
Ts.T=T;
Ts.E=E;
Ts.sC=sC;
Ts.cL=cL;
Ts.W=W;
setTimeout("Ts.Send();",20);
delete(F);delete(sC);delete(fm);delete(cL);delete(T);delete(E);
return false;
}
function sendMsg()
{ if (this.T=="" && this.F=="") {alert("你要对谁说?");return;}
cRld=1;
var sS="F="+code(this.F)+"&T="+code(this.T)+"&sC="+code(this.sC)+"&cL="+this.cL+"&E="+this.E;
sS=URLEncoding(sS)
var oSend=new ActiveXObject("Microsoft.XMLHTTP");
oSend.open("POST","say.asp",false);
oSend.setRequestHeader("Content-Length",sS.length);
oSend.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");//没有这句就玩不转拉
oSend.send(sS);
var oDom=new ActiveXObject("Microsoft.XMLDOM");
oDom.async=false;
body=bytes2BSTR(oSend.responseBody);
// alert(body);
oDom.loadXML(body);
if(oDom.parseError.errorCode != 0)
{ alert(body);
alert("Sorry.出现了错误");
return }
else
{var aryF=oDom.documentElement.selectNodes("F");
var aryT=oDom.documentElement.selectNodes("T");
var arysC=oDom.documentElement.selectNodes("sC");
var arycL=oDom.documentElement.selectNodes("cL");
var arysJ=oDom.documentElement.selectNodes("sJ");
var aryE=oDom.documentElement.selectNodes("E");
var tmpHtml="";
var img="";
lengt=aryF.length;
for(var i=0;i";
tmpHtml=""+document.oT.userface.options[aryE(i).text].value+rp2(arysC(i).text,
"{^amp]","&")+" ["+arysJ(i).text+"]
";
tmpHtml=rp2(tmpHtml,"$F",""+img+aryF(i).text+"");
tmpHtml=rp2(tmpHtml,"$T",""+aryT(i).text+"");
if (aryF(i).text==Me || aryT(i).text==Me)
{ parent.my.Ly.innerHTML+=tmpHtml;
} else
parent.pub.Ly.innerHTML+=tmpHtml;
}// end for
}
delete(aryF);delete(aryT);delete(arysC);delete(arycL);delete(arysJ);delete(aryE);
delete(oDom)
delete(oSend)
delete(tmpHtml)
}
function code(Str)
{var A = new RegExp("'","gi")
var B = new RegExp("\"","gi")
Str = Str.replace(/&/gi,"&")
Str = Str.replace(//gi,">")
Str = Str.replace(B,""")
Str = Str.replace(/&/gi,"{^amp]")
delete(A)
delete(B)
return(Str)
}
function rp2(Str,Fs,Is)
{while (Str.indexOf(Fs)!=-1)
Str = Str.replace(Fs,Is)
delete(A);
return(Str)
}

#############续send.asp#################
var oRDom=new ActiveXObject("Microsoft.XMLDOM");
oRDom.async=false;
var Path="reload.asp";
var tCok;
function Clock()
{cRld++;
if (cRld>10){cRld=1;GetXmlData();}
clearTimeout(tCok);
tCok=setTimeout("Clock()",1000);
}
function GetXmlData() //这就是刷新用户XML数据的函数
{cRld=1;
oRDom.load(Path)
if(oRDom.parseError.errorCode != 0)
{alert("Sorry.出现了错误");
return }
else
{
var aryF=oRDom.documentElement.selectNodes("F");
var aryT=oRDom.documentElement.selectNodes("T");
var arysC=oRDom.documentElement.selectNodes("sC");
var arycL=oRDom.documentElement.selectNodes("cL");
var arysJ=oRDom.documentElement.selectNodes("sJ");
var aryE=oRDom.documentElement.selectNodes("E");
var tmpHtml="";
lengt=aryF.length;
for(var i=0;i";
tmpHtml=""+eV[aryE(i).text*2]+rp2(arysC(i).text,"{^amp]","&")+" ["+arysJ(i).text+"]
";
tmpHtml=rp2(tmpHtml,"$F",""+img+aryF(i).text+"");
tmpHtml=rp2(tmpHtml,"$T",""+aryT(i).text+"");
if (aryF(i).text==Me || aryT(i).text==Me)
{ parent.my.Ly.innerHTML+=tmpHtml;
} else
parent.pub.Ly.innerHTML+=tmpHtml;
}
}
delete(aryF);delete(aryT);delete(arysC);delete(arycL);delete(arysJ);
}
function towho(un){
document.say.T.options[0].value=un;
document.say.T.options[0].text=un;
document.say.says.focus();return;}








" readonly style="BACKGROUND-COLOR: #76aadd; BORDER-BOTTOM: 0px double; BORDER-LEFT: 0px double; BORDER-RIGHT: 0px double; BORDER-TOP: 0px double; COLOR: #ffffff; TEXT-ALIGN: right">
对  
大家
说:






滚屏




默认颜色
黑色沉静
红色热情
蓝色开朗
桃色浪漫
绿色青春
青色清爽
紫色拘谨
暗夜兴奋
深蓝忧郁
卡其制服
镏金岁月
湖波荡漾
发亮蓝紫
爱的暗示
墨绿深沉
灰色轨迹
伦敦灰雾



for(var iF=0;iF"+eV[iF+1]);
}

私聊




  


write();
GetXmlData();
Clock();

[ 本帖最后由 wang23_26_02 于 2006-2-10 01:45 编辑 ]

TOP

TOP

发新话题