Ajax的DWR框架三在线交流

dwr.jar
1.test.html
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
 <link rel="stylesheet" type="text/css" href="generic.css" />
<title>DWR入门--DWR后台调用页面组件  蓝杰信息@NetJava.cn</title>
</head>
<!--引入dwr的js脚本-->
  <script   src="dwr/interface/ChatWithSamePage.js"></script>
  <script   src="dwr/engine.js" /></script>
  <script    src="dwr/util.js" /></script>
<script type="text/javascript">
    function sendMessage() {
      ChatWithSamePage.sendWebMessage(dwr.util.getValue("text"));
    }
  </script>
<body>
<pre>
发送内容:
<input type="text"  id="text" οnkeypress="dwr.util.onReturn(event, sendMessage)"/>
<input type="button" value="Send" οnclick="sendMessage();"/>
<hr>
<ul id="chatlog"  style="list-style-type:none;">
 </ul>
 </pre>
 
</body>
</html>
2.ChartwithSamPage.java
package cn.netjava.dwr;
import java.util.*;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.proxy.dwr.Util;
import org.directwebremoting.ScriptSession;
/**
 * 通知消息调用java对象
 */
public class ChatWithSamePage {
/**
     * @param text The new message text to add
     */
    public void sendWebMessage(String text)
    {
      WebContext wctx = WebContextFactory.get();
      String remoteAdd= wctx.getHttpServletRequest().getRemoteAddr();
      text="来自"+remoteAdd+"的朋友说:"+text;
        // Make sure we have a list of the list 10 messages
        if (text != null && text.trim().length() > 0)
        {
            messages.addFirst(new WebMessage(text));
            while (messages.size() > 10)
            {
                messages.removeLast();
            }
        }
        //将队列中的消息通知到所有客户端
        notifyAllClient(wctx,messages);
      
    }
   
    public void notifyAllClient(WebContext wctx,List msgQueue){
      String currentPage = wctx.getCurrentPage();
         // Clear the input box in the browser that kicked off this page only
         Util utilThis = new Util(wctx.getScriptSession());
         utilThis.setValue("text", "");
         // For all the browsers on the current page:
         Collection<ScriptSession> sessions = wctx.getScriptSessionsByPage(currentPage);
         Util utilAll = new Util(sessions);
         // Clear the list and add in the new set of messages
         utilAll.removeAllOptions("chatlog");
         utilAll.addOptions("chatlog", messages, "text");
    }
   
    /**
     * The current set of messages
     */
    private LinkedList messages = new LinkedList();
}
_________________________________________________
WebMessage.java
package cn.netjava.dwr;
public class WebMessage {
/**
     * @param newtext the new message text
     */
    public WebMessage(String newtext)
    {
        text = newtext;
        if (text.length() > 256)
        {
            text = text.substring(0, 256)+". . .";
        }
    }
    /**
     * @return the message id
     */
    public long getId()
    {
        return id;
    }
    /**
     * @return the message itself
     */
    public String getText()
    {
        return text;
    }
    /**
     * When the message was created
     */
    private long id = System.currentTimeMillis();
    /**
     * The text of the message
     */
    private String text;
}
3.web.xml和上节的一样不变。
 WEB-INF/dwr.xml
<dwr>
     <!-- 聊天消息调用接口 -->
    <create creator="new" javascript="ChatWithSamePage" scope="application">
      <param name="class" value="cn.netjava.dwr.ChatWithSamePage"/>
    </create>                     
    <convert match="cn.netjava.dwr.WebMessage" converter="bean"/>
  </allow>
</dwr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值