一:jsp(显示层代码块)如下:
聊天页面显示 代码如下:
index.jsp
<script type="text/javascript" src="/dwr-comet/js/jquery/jquery- 1.2.6.pack.js">
</script>
<script type="text/javascript" src="/dwr-comet/dwr/interface/myjs.js">
</script>
<script type="text/javascript" src="/dwr-comet/dwr/engine.js">
</script><script type="text/javascript" src="/dwr-comet/dwr/util.js">
</script>
聊天页面显示 代码如下:
<script>
function login()
{ var from = jQuery("#username").val();
if (from=="")
{ alert("请输入用户名"); retun;
}
myjs.login(jQuery("#username").val(),
function(x){
if (!x) document.getElementById("loginmsg").innerHTML = "登陆成功"; else document.getElementById("loginmsg").innerHTML = "用户已在别处登陆"; });
}
//添加用户列表
function addUser(arry)
{ var from = jQuery("#username").val(); if (jQuery.trim(from)=="" || arry.length==0) return; dwr.util.removeAllOptions("users"); dwr.util.addOptions("users", arry,"id","name");
}
//发送 function send()
{ var from = jQuery("#username").val(); var toId = jQuery("#users").val(); var toName = jQuery("#users>option:selected").html(); var msg = jQuery("#text").val(); var time = new Date(); myjs.sendMsg(toId,toName,msg,function(x)
{ if (x) { var mm = document.getElementById("msg"); var f = document.createElement("div"); f.innerHTML = "<font color=red>("+time.toLocaleString()+")"+from+"对"+toName+"说:"+ ")</font><br>"; mm.appendChild(f); var m = document.createElement("div"); m.innerHTML = msg+"<br><br>"; mm.appendChild(m); } });
} //接收消息
function recv(from,to,msg,time) {
var mm = doc
ument.getElementById("msg"); var f = document.createElement("div"); f.innerHTML = "<font color=red>"+time.toLocaleString()+from+"对"+to+"说"+ "</font><br>"; mm.appendChild(f); var m = document.createElement("div"); m.innerHTML = msg+"<br><br>"; mm.appendChild(m);
}
//退出事件
window.onbeforeunload = function(){ jQuery.post("ExitGameServlet"); } </script>