dwr公聊有问题???
package com.newer.data;
import java.util.Collection;
import java.util.LinkedList;
import org.directwebremoting.*;
import org.directwebremoting.proxy.dwr.*;
/**
* <dwr 用于在线公聊>
* 通知消息调用的java对象
* @author Administrator
*
*/
public class ChatWithSamePage {
private LinkedList<WebMessage> message = new LinkedList<WebMessage>();
/**
* add message
* @param text
*/
public void sendWebMessage(String text){
/**
* 当前的上下文环境
*/
WebContext wctx = WebContextFactory.get();
/*获取客户端IP*/
String ip = wctx.getHttpServletRequest().getRemoteAddr();
System.out.println("客户端IP:"+ip);
text = "来自" + ip +"的朋友说:" + text;
if(text != null && text.trim().length() > 0){
message.addFirst( new WebMessage(text));
System.out.println(text);
while(message.size()>10){
message.removeLast();
}
}
//将队列中的消息通知发给所有端
notifyAllClient(wctx,message);
}
private void notifyAllClient(WebContext wctx, LinkedList message) {
// TODO Auto-generated method stub
/*获取当前页的路径*/
String currentPageURL = wctx.getCurrentPage();
System.out.println("currentPage:"+currentPageURL);
//清空输入文本档
Util utilThis = new Util(wctx.getScriptSession());
utilThis.setValue("text", "");
//取出所有浏览器的当前页
Collection<ScriptSession> session = wctx.getScriptSessionsByPage(currentPageURL);
System.out.println(session);
Util utilall = new Util(session);
System.out.println(utilall);
//清空列表加入新的列表
utilall.removeAllOptions("chatlog");
utilall.addOptions("chatlog", message,"text");
}
}
package com.newer.data;
/**
* 发送信息类
* @author JunQing
* date: 2010-9-28 12:40 WED
*/
public class WebMessage {
private long id = System.currentTimeMillis();
private String text;
/**
* @return the id
*/
public long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(long id) {
this.id = id;
}
/**
* @return the text
*/
public String getText() {
return text;
}
/**
* @param text the text to set
*/
public void setText(String text) {
this.text = text;
}
public WebMessage(String text){
this.text = text;
if(text.length()>256){
text = text.substring(0,256)+"....";
}
}
public WebMessage(){}
}
*******************************web.xml*************************
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>crossDomainSessionSecurity</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>maxWaitingThreads</param-name>
<param-value>100</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
</web-app>
/**************************************dwr.xml*******************************/
<?xml version="1.0" encoding="UTF-8"?>
<dwr>
<allow>
<create creator="new" javascript="chatWithSamePage" scope="application">
<param name="class" value="com.newer.data.ChatWithSamePage"/>
</create>
</allow>
<convert match="com.newer.data.WebMessage" converter="bean"/>
</dwr>
/************************************index.jsp******************************/
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type='text/javascript' src='/dwrsendmsg/dwr/interface/chatWithSamePage.js'></script>
<script type='text/javascript' src='/dwrsendmsg/dwr/engine.js'></script>
<script type='text/javascript' src='/dwrsendmsg/dwr/util.js'></script>
<script type="text/javascript">
function sendMessage(){
chatWithSamePage.sendWebMessage(document.getElementById("text").value);
}
</script>
</head>
<body>
<pre>c
发送内容:
<input type="text" id="text" onkeypress="dwr.util.onReturn(event,sendMessage)"/>
<input type="button" value="send" onclick="sendMessage()"/>
<hr>
<select id="chatlog" multiple="multiple">
</select>
</pre>
</body>
</html>