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

DWR3.0推送
最近在学DWR3.0推送的时候
  String page = WebContextFactory.get().getContextPath() + "/index.jsp";
  Browser.withPage(page, new Runnable() {
  public void run() {
  Util.setValue("clockDisplay", "发布消息啦!");
  }
  });

  就是说我在chat.jsp发布一个消息时 如何才能将我要发布的消息推送到index.jsp页面上!
  好像每次测试时  
  运行上面的方法时不运行到run方法
  但是运行
  // 推给所有用户
Browser.withAllSessions(new Runnable() {
public void run() {
Util.removeAllOptions("users");
Util.addOptions("users", users, "name");
Util.removeAllOptions("receiver");
Util.addOptions("receiver", users, "name");
}
});


  // 推给指定用户
Browser.withCurrentPageFiltered(new ScriptSessionFilter() {
public boolean match(ScriptSession session) {
if (session.getAttribute("user") == null)
return false;
else
return ((User) session.getAttribute("user")).getName()
.equals(receiverid);
}
}, new Runnable() {
public void run() {
Collection<ScriptSession> colls = Browser.getTargetSessions();
for (ScriptSession scriptSession : colls) {
scriptSession.addScript(initFunctionCall(
"dwr.util.setValue", "sender", sender));
scriptSession.addScript(initFunctionCall(
"dwr.util.setValue", "msg", msg));
}
ScriptBuffer script = new ScriptBuffer();
script.appendScript("showMessage(").appendData(msg)
  .appendScript(");");
}
});

  这两个方法的时候没有问题 请问大家 是不是推送到指定页面要加什么特殊的配置吗? 


------解决方案--------------------
帮你顶下,期待高手
------解决方案--------------------
DefaultWebContext wctx = (DefaultWebContext) WebContextFactory.get(); // 这里是获取WebContext上下文
String currentPage = wctx.getCurrentPage();
Collection<ScriptSession> sessions = wctx.getScriptSessionsByPage(currentPage);
这种使用的是当前页面推送,也就是你的chat.jsp
如果你要推送的index.jsp,上面2步就不需要了,直接把currentPage 修改为 /项目名称/index.jsp 即可,注意要以“/”开头。
------解决方案--------------------
楼上正解
DefaultWebContext 及 getScriptSessionsByPage才是要点
------解决方案--------------------
首先,回答是可以的。
但是前提是要知道index的scriptSessionID。
DWR推送是通过scriptSessionID实现的,每个页面都会生成一个这样的ID,这样DWR可以通过它推往相关页面。这个页面是在引用相关DWR就生成的。