十、 信息接收功能
客户端:
function getChat() //获得聊天信息
{
if(document.getElementById("chatmain").contentWindow.document.body)
{ loadXML("GET","getChat.asp?frompage="+frompage+"a&b="+b+"&nc="+
}
objTimerout=window.setTimeout("getChat()",1000);
}
function getChatOk(xmlDom)
{
var cont="";
var chatinfo=xmlDom.responseXML.getElementsByTagName("chatinfo");
var len=chatinfo.length;
for(var i=0;i<len;i++)
{
var nc=chatinfo[i].childNodes[0].firstChild.data;
var msgcont=chatinfo[i].childNodes[1].firstChild.data;
var msgdate=chatinfo[i].childNodes[2].firstChild.data;
cont+="<div class=\"msg\"><div class=\"msgnc\">"+nc+":</div><div class=\"msgcont\">"+msgcont+" ["+msgdate+"]</div></div>";
}
if(document.getElementById("chatmain").contentWindow.document.body)
{ document.getElementById("chatmain").contentWindow.document.body.innerHTML="<div id=\"authorInfo\" class=\"chatroom\"><font color=\"#ff0000\">欢迎使用懒羊页面聊天系统v1.0<br/>开发人:懒羊<br/>开发时间:
document.getElementById("chatmain").contentWindow.document.body.scrollTop=document.getElementById("chatmain").contentWindow.document.body.scrollHeight;
}
var msginfo=xmlDom.responseXML.getElementsByTagName("msginfo");
if(document.getElementById("tsinfomain")&&msginfo[0])
{
document.getElementById("tsinfomain").value= msginfo[0].firstChild.data;
}
}
服务器端:getchat.asp
<% Response.Charset="gb2312" %>
<% Session.CodePage=936 %>
<!--#include file="conn.asp"-->
<%
b=request("b")
if instr(b,"Netscape") then
nc=StreamToStr(request("nc"))
frompage=StreamToStr(request("frompage"))
Response.write("<?xml version=""1.0"" encoding=""UTF-8""?>")
else
nc=del1(request("nc"))
frompage=del1(request("frompage"))
Response.write("<?xml version=""1.0"" encoding=""gb2312""?>")
end if
Response.ContentType = "text/XML"
Response.expires = 0
Response.expiresabsolute = Now() - 1
Response.addHeader "pragma", "no-cache"
Response.addHeader "cache-control", "private"
Response.CacheControl = "no-cache"
if session(nc)="" then
session(nc)=now() '获得当前时间
end if
set rs=server.CreateObject("adodb.recordset")
sql="select * from chatroom where frompage='"&frompage&"' and datediff('s','"&session(nc)&"',dateandtime)>0"
rs.open sql,conn,1,1
if not rs.bof or not rs.eof then
do while not rs.eof
content=content&"<chatinfo><nc>"&rs("nc")&"</nc><msgcont>"&rs("chatmsg")&"</msgcont><msgdate>"&formatdatetime(rs("dateandtime"),3)&"</msgdate></chatinfo>"
rs.movenext
loop
end if
rs.close
set rs=nothing
set rs=server.CreateObject("adodb.recordset")
sql="select * from msg where frompage='"&frompage&"' order by id desc"
rs.open sql,conn,1,1
if not rs.bof or not rs.eof then
do while not rs.eof
msg=msg&rs("msg")&chr(13)
rs.movenext
loop
end if
rs.close
set rs=nothing
conn.close
set conn=nothing
response.Write("<lanyang>"&content&"<msginfo>"&msg&"</msginfo></lanyang>")
%>
注:
1、if session(nc)="" then
session(nc)=now()
end if
当访客进入系统后,获得的只是从访客开始向后的聊天信息,而并非所有信息,因此我们就必须对访客进入系统的时间进行存储,而且因为同一台电脑可以打开多个页面,我们必须保证每个页面存储时间的变量唯一,因此我们通过根据唯一的呢称来设定变量,但由于改名以后呢称发生变化,因此我们要将旧呢称的时间信息存储到新的呢称变量中,这样就可以保证我们前面聊天记录的不丢失。
2、服务器端文件为asp生成的xml文件,其中主要包括chatinfo与msginfo两个节点,前者节点用来显示聊天信息,而后者则为操作提示信息。