在csdn上看到这么一个例子 
<init-param> 
<param-name>crossDomainSessionSecurity </param-name> 
<param-value>true </param-value> 
</init-param> 
<init-param> 
<param-name>pollAndCometEnabled </param-name> 
<param-value>true </param-value> 
</init-param> 
接收页面初始化 
window.onload = init; 
function init() { 
dwr.engine.setActiveReverseAjax(true); 
} 页面接收函数 
//服务器推的消息 
function receiveMessages(result) { 
........ 
} 服务器端推函数 /** 
* dwr为服务器端推而做的,应用到还原时候,异步回显执行结果 
*/ 
public void realTimeOperate(String result) { 
WebContext wctx = WebContextFactory.get(); 
ScriptBuffer sb = new ScriptBuffer(); 
sb.appendScript("receiveMessages(").appendData(result).appendScript( 
");"); 
ServerContext sctx = ServerContextFactory.get(wctx.getServletContext()); 
Collection <ScriptSession> pages = sctx 
.getScriptSessionsByPage("/receive.jsp"); 
for (ScriptSession session : pages) { 
session.addScript(sb); 


开始那段能明白,就是后面这段我有点不是很懂 
public void realTimeOperate(String result) { 
WebContext wctx = WebContextFactory.get(); 
ScriptBuffer sb = new ScriptBuffer(); 
sb.appendScript("receiveMessages(").appendData(result).appendScript( 
");"); 
ServerContext sctx = ServerContextFactory.get(wctx.getServletContext()); 
Collection <ScriptSession> pages = sctx 
.getScriptSessionsByPage("/receive.jsp"); 
for (ScriptSession session : pages) { 
session.addScript(sb); 


有几个问题,这个方法怎么样去触发?是不是反推送不用去触发? 
比如说我要每一段时间查一下数据库,有新数据就像客户端推送,应该怎样谢,上面的代码片段中我没研究明白 
最后一个问题就是怎样设置时间!~ 
请大家指教一下,谢谢大家!!!本来发在ajax那边...不过那边貌似人气不怎样.dwr也算是针对java的框架发在这边也应该说的过去...

解决方案 »

  1.   

    推送是用来推送即时消息的,也就是消息到了就送过去!要实现隔一段时间推送没有做过。 不过可以直接在页面上用setInterval(functionName, timer) 函数来实现吧,每隔一段时间调用一次你与后台交互的DWR函数。
      

  2.   

    我差不多弄明白了,当你需要服务器推的时候触发写好的方法,服务器将数据推出去
    但是现在又有几个新问题了public void realTimeOperate(String result) 

       WebContext wctx = WebContextFactory.get(); 
       ScriptBuffer sb = new ScriptBuffer(); 
       sb.appendScript("receiveMessages(").appendData(result).appendScript( ");"); 
       ServerContext sctx = ServerContextFactory.get(wctx.getServletContext()); 
       Collection <ScriptSession> pages = sctx.getScriptSessionsByPage("/receive.jsp"); 
       for (ScriptSession session : pages) 
       { 
         session.addScript(sb); 
       } 
    }上述方法,我如果用dwr去触发,一点问题都没有。但是我想在action去触发这个方法,例如我象数据库插入一条数据,调用此方法,页面刷新出新数据。
    我试了一下,调用的时候获取servercontext的时候,会报空指针,我换成了用request获取
    但是sctx.getScriptSessionsByPage("/receive.jsp");  这句又空指针了。。我百思不得其解
    请高手指教一下,谢谢!