商讯信箱
用户名: @
密  码:   注册|忘记密码
登录
个人用户经销商
信箱 E杂志
您的位置:首页 > 学院 > 正文       

十、 信息接收功能

客户端:

function getChat()  //获得聊天信息

{

if(document.getElementById("chatmain").contentWindow.document.body)

{          loadXML("GET","getChat.asp?frompage="+frompage+"a&b="+b+"&nc="+nc+"a",getChatOk);

}

  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/>开发时间:2007-6-22<br/>------------------------------------<br/></font></div>"+cont;

     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>")

%>

 

注:

1if session(nc)="" then  

session(nc)=now()  

end if

当访客进入系统后,获得的只是从访客开始向后的聊天信息,而并非所有信息,因此我们就必须对访客进入系统的时间进行存储,而且因为同一台电脑可以打开多个页面,我们必须保证每个页面存储时间的变量唯一,因此我们通过根据唯一的呢称来设定变量,但由于改名以后呢称发生变化,因此我们要将旧呢称的时间信息存储到新的呢称变量中,这样就可以保证我们前面聊天记录的不丢失。

2服务器端文件为asp生成的xml文件,其中主要包括chatinfomsginfo两个节点,前者节点用来显示聊天信息,而后者则为操作提示信息。

1 2 3 4 5 6 7 8 9 10 11 12
有问必答
【内容导航】
第1页:系统功能概述 第2页:界面的实现
第3页:界面效果及位置的实现 第4页:Ajax原理
第5页:数据库规划 第6页:系统登陆
第7页:在线访客统计 第8页:呢称修改功能
第9页:信息发送功能 第10页:信息接收功能
第11页:系统退出 第12页:还原按钮与最小化按钮
©版权所有。未经许可,不得转载。
[责任编辑:罗可龙] [我要挑错]
相关产品
本文相关产品
   没有相关产品