编程语言

img iuhxq

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

发表于2004/9/24 20:16:00  5176人阅读

主页面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">
<meta http-equiv="refresh" content="3600">
<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>
  <table width="768"  border="0" align="center" cellpadding="0" cellspacing="0">
<form name="form1" method="post" action="" onSubmit="return false;">
    <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="25" onChange="chat_user_change();">
          </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="30" wrap="VIRTUAL" id="log"></textarea></td>
    </tr>
    <tr>
      <td><select name="tophone" onChange="srcphone = this.value;"></select></td>
      <td><input name="content" type="text" id="content" size="90" 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>
</form>
  </table>
</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 chat_user_change()
{
 var i;
 for (i=0;i<document.form1.chat_user.options.length;i++)
 {
  if (document.form1.chat_user.options[i].selected)
  {
   var j;
   for (j=0;j<document.form1.tophone.options.length;j++)
    if (document.form1.tophone.options[j].text == document.form1.chat_user.options[i].text)
     break;
   if (j == document.form1.tophone.options.length)
   {
    document.form1.tophone.options.add(new Option(document.form1.chat_user.options[i].text,document.form1.chat_user.value));
   }
   document.form1.tophone.options[j].selected = true;
  }
 }
 srcphone = document.form1.chat_user.value;
}
function Send()
{
 try
 {
  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();
 }
 catch(e)
 {
  alert(e.description);
 }
}

返回聊天内容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 tophone is not null and 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 srcphone, userid, nickname = CASE LEFT(phone, 3) WHEN '133' THEN nickname + '·' WHEN '130' THEN nickname + '·' ELSE nickname END,roomid FROM dbo.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&"','0'")
'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
%>

阅读全文
0 0

相关文章推荐

img
取 消
img