<%@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
%>
文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




