先简单说明下
服务端
用程序生成需要的 xml 文件
客户端
利用 xmlhttp 或者 dso(注意状态) 定时刷新需要的数据
相对来说 dso 可以映射成 recordset 操作比较简单
以下是我简单写着玩的东西 不是完整部分 而且十分简陋
只是代码片段 但作为技术参考已经足够
需要 msxml 4.0
服务端 我写了两个构造函数 分别是 聊天内容 聊天用户
<script language="jscript" runat="server">
function slightboychat()
{
this.id;
this.xmldomelement = server.createobject("msxml2.freethreadeddomdocument.4.0")
this.add = function(name, content, append)
{
var root = this.xmldomelement.documentelement;
var newitem = this.xmldomelement.createelement("item");
var newitemname = this.xmldomelement.createelement("name");
var newitemnamevalue = this.xmldomelement.createcdatasection(name);
newitemname.appendchild(newitemnamevalue)
var newitemcontent = this.xmldomelement.createelement("content");
var newitemcontentvalue = this.xmldomelement.createcdatasection(content);
newitemcontent.appendchild(newitemcontentvalue)
var newitemappend = this.xmldomelement.createelement("append");
newitemappend.text = append;
newitem.appendchild(newitemname);
newitem.appendchild(newitemcontent);
newitem.appendchild(newitemappend);
if ( root.childnodes.length > 10 )
{
root.removechild(root.firstchild)
}
root.appendchild(newitem)
}
this.save = function()
{
application("chatcontent") = this.xmldomelement.xml;
}
this.guid = function(pushguid)
{
if ( pushguid.count > 0 )
{
this.id = pushguid;
}
else
{
this.id = "";
}
}
this.xml = function()
{
if ( this.id != "" )
{
var xmldomelementstring = "<?xml version=\"1.0\" encoding=\"gb2312\"?><chat>";
var items = chat.xmldomelement.selectnodes("//item[append > "+ this.id +"]")
for (var item = items.nextnode(); item; item = items.nextnode())
{
xmldomelementstring += item.xml;
}
xmldomelementstring += "</chat>";
if ( items.length > 0 )
{
return xmldomelementstring;
}
else
{
return;
}
}
else
{
return this.xmldomelement.xml.replace("<?xml version=\"1.0\"?>","<?xml version=\"1.0\" encoding=\"gb2312\"?>");
}
}
this.load = function()
{
if ( application("chatcontent") == "" | typeof(application("chatcontent")) == "undefined" )
{
application("chatcontent") = "<?xml version=\"1.0\" encoding=\"gb2312\"?><chat><item><name>slightboy</name><content>欢迎 ^^</content><append>"+ new date().gettime() +"</append></item></chat>";
}
this.xmldomelement.loadxml(application("chatcontent"));
}
this.empty = function()
{
application("chatcontent") = "<?xml version=\"1.0\" encoding=\"gb2312\"?><chat><item><name>slightboy</name><content>欢迎 ^^</content><append>"+ new date().gettime() +"</append></item></chat>";
}
this.load();
}
function slightboychatlist()
{
this.xmldomelement = server.createobject("msxml2.freethreadeddomdocument.4.0")
this.add = function(name, level)
{
var root = this.xmldomelement.documentelement;
var newitem = this.xmldomelement.createelement("item");
var newitemname = this.xmldomelement.createelement("name");
var newitemnamevalue = this.xmldomelement.createcdatasection(name);
newitemname.appendchild(newitemnamevalue)
var newitemlevel = this.xmldomelement.createelement("level");
var newitemlevelvalue = this.xmldomelement.createcdatasection(level);
newitemlevel.appendchild(newitemlevelvalue)
var newitemappend = this.xmldomelement.createelement("append");
newitemappend.text = append;
newitem.appendchild(newitemname);
newitem.appendchild(newitemlevel);
root.appendchild(newitem)
}
this.save = function()
{
application("chatuser") = this.xmldomelement.xml;
}
this.xml = function()
{
return this.xmldomelement.xml.replace("<?xml version=\"1.0\"?>","<?xml version=\"1.0\" encoding=\"gb2312\"?>");
}
this.load = function()
{
if ( application("chatuser") == "" | typeof(application("chatuser")) == "undefined" )
{
application("chatcontent") = "<?xml version=\"1.0\" encoding=\"gb2312\"?><chat></chat>";
}
this.xmldomelement.loadxml(application("chatuser"));
}
this.remove = function()
{
var removetimer = new date().gettime();
removetimer -= 300000;
var root = this.xmldomelement.documentelement;
var removenodes = root.selectnodes("//item[append < "+ removetimer +"]");
while (removenodes.peeknode() != null)
{
removenodes.removenext();
}
}
this.load();
}
</script>
客户端聊天内容显示页面
<html>
<meta http-equiv=content-type content=text/html; charset=gb2312>
<script language="javascript">
function slightboychat()
{
this.guid = "";
this.recordset;
var timer = new date();
this.checkstate = function()
{
if ( xmlchat.readystate == "complete" )
{
this.recordset = xmlchat.recordset;
this.item();
}
}
this.item = function()
{
try
{
var pushstring;
this.recordset.movelast();
if ( this.recordset("append").value != this.guid)
{
this.guid = this.recordset("append").value;
}
else
{
this.recordset = null;
return;
}
this.recordset.movefirst();
while (!this.recordset.eof)
{
timer.settime(this.recordset("append").value)
pushstring = this.recordset("name").value +" 说: "+ this.recordset("content").value +" <small>"+ timer.tolocalestring() +"</small>";
this.push(pushstring);
this.recordset.movenext();
}
this.recordset = null;
}
catch(e)
{
return;
}
}
this.push = function(pushstring)
{
var chatitem = document.createelement("li");
chatitem.innerhtml = pushstring;
chatlist.insertadjacentelement("afterbegin",chatitem);
}
this.load = function()
{
xmlchat.src = "background.asp?guid="+ this.guid;
}
this.remove = function()
{
try
{
var removelength = chatlist.children.length;
for (var i = 0; i < removelength; i++)
{
chatlist.removechild(chatlist.children(0));
}
}
catch(e)
{
}
}
}
var chat = new slightboychat();
window.setinterval("chat.load()", 5000);
</script>
</head>
<body>
<xml id="xmlchat" src="background.asp" onreadystatechange="chat.checkstate()"></xml>
<ul id="chatlist" style="list-style: none;margin: 0px;">
<li>欢迎访问 slightboy xml chatroom…<small>(slightboy)</small></li>
</ul>
</body>
</html>
