主页面index.asp:
<%@LANGUAGE=VBSCRIPT CODEPAGE=936%>
<!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd>
<!--#include file=conn.asp-->
<!--#include file=chklogin.asp-->
<html>
<head>
<meta http-equiv=Content-Type content=text/html; charset=gb2312>
<META HTTP-EQUIV=PRAGMA CONTENT=NO-CACHE>
<title>聊天室</title>
<link href=style.css rel=stylesheet type=text/css>
</head>
<body>
<script language=javascript>
var t_room,t_log,t_user;
var phone,srcphone,province,content;
var room,log,user,myroomid=0,pid;
log = Array();
user = Array();
<%
set rs=conn.execute(select top 1 chat_id as id from chat_log where chat_id in (select top 50 chat_id from chat_log order by chat_id desc) order by chat_id)
response.Write(pid=&rs(id)&;)
%>
</script>
<script language=javascript src=function.js></script>
<form name=form1 method=post action= onSubmit=return false;>
<table width=100% border=0 cellspacing=0 cellpadding=0>
<tr>
<td width=11% valign=top><div align=center>
<select id=chat_room onChange=Change_room();></select><br>
<select id=chat_user name=select size=30 onChange=srcphone = this.value;></select><br>
<span onClick=Load_room(); style=cursor:hand;>刷新房间列表</span><br>
<span onClick=Load_user(); style=cursor:hand;>刷新用户列表</span></div></td>
<td width=89% valign=top><textarea name=log rows=35 wrap=VIRTUAL id=log></textarea></td>
</tr>
<tr>
<td> </td>
<td><input name=content type=text id=content size=100 maxlength=140 onKeyDown=if (event.keyCode==13) {Send();}>
<input name=SendToServer type=button id=SendToServer onClick=Send(); value=发送>
<input type=button name=Submit value=退出 onClick=window.location.href='logout.asp';></td>
</tr>
</table>
</form>
</body>
</html>
非常关键的函数function.js:
// javascript Document
window.onload=function init()
{
Load_log();
Load_user();
Load_room();
}
function GetPage(url)
{
var xml = new ActiveXObject(Microsoft.XMLHTTP);
xml.open(GET,url,false);
xml.send();
return unescape(xml.ResponseText);
}
function PostData(url,Body)
{
var xml = new ActiveXObject(Microsoft.XMLHTTP);
xml.open(POST,url,false);
xml.setRequestHeader(Content-Type,application/x-www-form-urlencoded)
xml.setRequestHeader(Content-Length,escape(Body).length)
xml.send(Body);
}
function Load_room()
{
var value = GetPage(chat_room.asp);
var str = value.split(#);
var id,roomname,temp;
room = new ActiveXObject(Scripting.Dictionary);
var i=str.length-1;
while(i-- >0)
{
temp = str[i].split(*);
id = temp[0];
roomname = temp[1];
room.Add(id,roomname);
}
Show_room();
//t_room = setTimeout(Load_room();,60000);
}
function Show_room()
{
var i;
for(i=document.form1.chat_room.options.length;i>0;i--)
{
document.form1.chat_room.options.remove(i-1);
}
document.form1.chat_room.options.add(new Option(所有,0));
a = (new VBArray(room.Items())).toArray();
for (i in a)
{
document.form1.chat_room.options.add(new Option(a[i],i));
}
document.form1.chat_room.selectedIndex = myroomid;
}
function Load_user()
{
var value = GetPage(chat_user.asp);
var str = value.split(#);
var userid,nickname,roomid,temp;
var i;
i = user.length-1;
while(i-- >=0)
{
user.pop();
}
i = str.length-1;
while(i-- >0)
{
temp = str[i].split(*);
userid = temp[0];
nickname = temp[1];
roomid = temp[2];
user.push(userid,nickname,roomid);
}
Show_user();
//t_user = setTimeout(Load_user();,60000);
}
function Show_user()
{
var i;
for(i=document.form1.chat_user.options.length;i>0;i--)
{
document.form1.chat_user.options.remove(i-1);
}
document.form1.chat_user.options.add(new Option(所有人,278810));
i = user.length-1;
while(i>=0)
{
if (user[i]==myroomid || myroomid==0)
{
document.form1.chat_user.options.add(new Option(user[i-1],user[i-2]));
}
i -=3;
}
document.form1.chat_user.selectedIndex = myroomid;
}
function Load_log()
{
var value=GetPage(chat_log.asp?id=+pid);
var str = value.split(#);
var id,roomid,msgBody;
var i=str.length-1;
while(i-- >0)
{
temp = str[i].split(*);
id = temp[0];
roomid = temp[1];
msgBody = temp[2];
log.push(roomid,msgBody);
pid = id;
}
if (str.length>1)
{
Show_log();
}
t_log = setTimeout(Load_log();,5000);
}
function Show_log()
{
var i=log.length-1;
var str=;
while(i>=0)
{
if (log[i-1]==myroomid || myroomid==0)
{
str += log[i].toString() + \n;
}
i -=2;
}
document.form1.log.value = str;
}
function Change_room()
{
myroomid=document.form1.chat_room.selectedIndex;
//clearTimeout(t_room);
//clearTimeout(t_user);
//clearTimeout(t_log);
Show_room();
Show_user();
Show_log();
}
function Send()
{
var Body = content= + escape(document.form1.content.value) + &srcphone= + escape(srcphone);
//Body = escape(Body);
PostData(chat_send.asp,Body);
//GetPage(chat_send.asp?+Body);
document.form1.content.value = ;
document.form1.content.Focus();
}
返回聊天内容chat_log.asp:
<!--#include file=conn.asp-->
<!--#include file=chklogin.asp-->
<%
Response.Expires = -1
Response.ExpiresAbsolute = Now() - 1
Response.cachecontrol = no-cache
if isnumeric(request(id)) then
set rs=conn.execute(select top 50 * from chat_log where chat_id>&request(id)& order by chat_id desc)
while not rs.eof
response.Write(escape(rs(chat_id)&*&rs(roomid)&*&rs(sendtime)&rs(msgBody)&# ))
rs.movenext
wend
end if
%>
取得房间列表chat_room.asp:
<!--#include file=conn.asp-->
<!--#include file=chklogin.asp-->
<%
Response.Expires = -1
Response.ExpiresAbsolute = Now() - 1
Response.cachecontrol = no-cache
set rs=conn.execute(select * from chat_room order by id desc)
while not rs.eof
response.Write(escape( rs(id)&*&rs(roomname)&# ))
rs.movenext
wend
%>
负责取得在线用户chat_user.asp:
<!--#include file=conn.asp-->
<!--#include file=chklogin.asp-->
<%
Response.Expires = -1
Response.ExpiresAbsolute = Now() - 1
Response.cachecontrol = no-cache
set rs=conn.execute(select * from chat_user where state=1 order by userid)
while not rs.eof
response.Write(escape( rs(srcphone)&rs(userid)&*&rs(nickname)&*&rs(roomid)&# ))
rs.movenext
wend
%>
负责发送聊天内容chat_send.asp:
<!--#include file=conn.asp-->
<!--#include file=chklogin.asp-->
<%
'exec chat '13500000000','ME','278810','571','1'
phone=session(phone)
province=session(province)
content=Request(content)
srcphone=Request(srcphone)
conn.execute(exec chat '&phone&','&content&','&srcphone&','&province&','1')
'savefile chat '&phone&','&content&','&srcphone&','&province&','1'
'function savefile(str)
' set fso=server.CreateObject(scripting.filesystemobject)
' set f1=fso.opentextfile(server.MapPath(./test.txt),8,true)
' f1.write(str)
' f1.close
' set fso=nothing
'end function
%>