日期:2014-05-17  浏览次数:20695 次

dwr为甚麽不能再不同页面同步消息,总是延时?
问题是这样的

我首先打开 2个网页http://localhost/chat/test.jsp

然后再其中一个输入1在这个网页显示啦,另一个没有显示,
然后我在第二个网页输入2,然后显示 1 2  
也就是把第一次的也显示啦,为甚麽我第一次的时候没有同步呢?




test.jsp
HTML code

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/Chat.js'></script>
 <script type='text/javascript' src='<%=request.getContextPath()%>/dwr/engine.js'></script>
 <script type='text/javascript' src='<%=request.getContextPath()%>/dwr/util.js'></script>
 <script type='text/javascript' src='js/chat.js'></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>chat</title>
</head>
<body onload="init()">
    <textarea readonly="readonly" rows="20" cols="30"  id="content">
    </textarea>
    <input type="text" id="input"  />
    <button onclick="send()">发送</button>
</body>
</html>


chat.js
JScript code

/**
 * 
 */
//测试是否全为空格
init=function(){
    
    dwr.engine.setActiveReverseAjax =true;
};
isEmpty=function(string){
    return string==""||string.match(/[^\s]/)==null;
};
send=function(){
    var input=dwr.util.getValue("input");
    if(!isEmpty(input)){
        Chat.send(input);
    }
};
update=function(messageInformation){
    
//    var last=dwr.util.getValue("content");
//    dwr.util.setValue("content",last+"\n"+sendMessage);
    alert(messageInformation.message);
    var last=document.getElementById("content").innerHTML;
    if(last!=null){
        last+="\n";
    }
    var totalMessage=messageInformation.ip+":"+messageInformation.message+"\n"+"  "+messageInformation.date;
    document.getElementById("content").innerHTML=last+totalMessage;
};



Chat。java
Java code

package mychat;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.directwebremoting.*;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.annotations.ScriptScope;

@RemoteProxy(scope=ScriptScope.APPLICATION)
public class Chat  {

    public Chat() {
        super();
        // TODO Auto-generated constructor stub
//        Util.execute(this);
    }

    @RemoteMethod
    public  void send(final String sendMessage) {
        // TODO Auto-generated method stub
        String ip=WebContextFactory.get().getHttpServletRequest().getLocalAddr();
        final MessageInformation mi=new MessageInformation(ip,sendMessage,new SimpleDateFormat("h:mm a").format(new Date()).toString());
        Browser.withCurrentPage((new Runnable() {
            public void run() {
                System.out.println(mi);
                ScriptSessions.addFunctionCall("update", mi);
            }
        }));
    }
    
}
































<