2011-05-31 27 views
-1

2つのWebページ(http://localhost/chat/test.jsp)を開いているうちに、最初のページからメッセージを送信していますが、2番目のページはすぐに受信できません。DWRはなぜ非同期ですか?私に何ができる?

最初のページが送信されたメッセージを受信できるのは、2番目のメッセージからメッセージを送信したときだけです。これについて私は何ができますか?

test.jsp

<%@ 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()">send</button> 
</body> 
</html> 

chat.js

dwr.engine.setAsync=false; 
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"); 
    if(!isEmpty(last)){ 
     last+="\n"; 
    } 
    var totalMessage=messageInformation.ip+":"+messageInformation.message+"\n"+" "+messageInformation.date; 
    dwr.util.setValue("content",last+totalMessage); 
}; 

Chat.java:何かが起こるときにサーバーがページを知らせる持っている

package mychat; 

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

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) { //i have tried 2 methods,but failed. 
//  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); 
//   } 
//  })); 
// } 
    public void send(final String sendMessage) { 
     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() { 
      ScriptBuffer script = createMyScriptBuffer(mi); 
      Collection<ScriptSession> sessions = Browser.getTargetSessions(); 
      for (ScriptSession scriptSession : sessions) { 
       System.out.println(script); 
       scriptSession.addScript(script); 
      } 
      } 
     }); 
    } 
    private ScriptBuffer createMyScriptBuffer(MessageInformation messageInformation) { 
     // TODO Auto-generated method stub 

    String str="var last=dwr.util.getValue(\"content\");"+ 
    "if(!isEmpty(last)){"+ 
    " last+=\"\n\";"+ 
    "}"+ 
    "var totalMessage="+messageInformation.ip+":"+messageInformation.message+"\n"+" "+messageInformation.date+";"+ 
    "dwr.util.setValue(\"content\",last+totalMessage);"; 
    return new ScriptBuffer().appendCall("update", messageInformation); 

    } 
} 
+0

DWRはAJAXを実装しています。 AJAXのAは非同期を表します。 –

答えて

0

良い方法は、ポーリングを使用することです。これを行う方法とhttp接続を開いたままにするなどのその他の潜在的な解決策については、reverse ajaxを参照してください。

関連する問題