当前位置:编程学习 > asp >>

无刷新聊天室(短信陪聊程序)

答案:主页面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=></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>&nbsp;</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=> </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

上一个:学习笔记(7.8) -- 对服务器控件应用样式
下一个:popup的两种方法

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,