微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

dwr3.0反ajax消息推送

web.xml
<!-- dwr ajax -->
 <servlet>
  <servlet-name>dwr-invoker</servlet-name>
  <servlet-class>
   org.directwebremoting.spring.DwrSpringServlet
  </servlet-class>
  <init-param>
   <param-name>debug</param-name>
   <param-value>false</param-value>
  </init-param>
  <init-param>
   <param-name>activeReverseAjaxEnabled</param-name>
   <param-value>true</param-value>
  </init-param>
  <!-- cause dwr js load error? --> 
  <init-param> 
   <param-name>config</param-name> 
   <param-value>WEB-INF/config/dwr/dwr.xml</param-value> 
  </init-param>
  <load-on-startup>4</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>dwr-invoker</servlet-name>
  <url-pattern>/dwr/*</url-pattern>
 </servlet-mapping>

/**
	 *消息推送。 向页面推送消息
	 * @param msg
	 */
	public void send(Map<String,String> map) {
		final Map<String,String> autoMsg =  map;
		String page = ServerContextFactory.get().getcontextpath() +"/main.jsp";
		browser.withPage(page,//对httpSession和scriptSession进行判断,将消息推送给不同用户
//		browser.withPageFiltered(page,new ScriptSessionFilter() {
//			@Override
//			public boolean match(ScriptSession session) {
//				HttpSession ss = WebContextFactory.get().getSession();  
//				//无session,不推送消息
//				if (ss == null)
//					return false;
//				else {
//					return true;
//				}
//			}
//		},new Runnable() {
			ScriptBuffer sBuffer = new ScriptBuffer();
			public void run() {
				sBuffer.appendCall("show",autoMsg);
				Collection<ScriptSession> sessions = browser.getTargetSessions();
				for (ScriptSession scriptSession : sessions) {
					scriptSession.addScript(sBuffer);
				}
			}
		});
	}
/**
	 *scriptSession创建和注销
	 */
 private static final long serialVersionUID = -201306202407420071L;
  public void init()	throws servletexception {
         Container container = ServerContextFactory.get().getContainer();
         ScriptSessionManager manager = container.getBean(ScriptSessionManager.class);
         ScriptSessionListener listener = new ScriptSessionListener() {
              public void sessionCreated(ScriptSessionEvent ev) {
              	HttpSession session = WebContextFactory.get().getSession();
              	String userId = (String)session.getAttribute("loginUserId");
              	System.out.println("a ScriptSession is created!");
              	ev.getSession().setAttribute("userId",userId );
                	}
              public void sessionDestroyed(ScriptSessionEvent ev) {
              	System.out.println("a ScriptSession is distroyed");
              	}
         };
         manager.addScriptSessionListener(listener);
  }

//main.jsp<script type="text/javascript"> $(function(){          //允许使用推送技术          dwr.engine.setActiveReverseAjax(true);     });       //后台推送的时候调用        function show(dbsyMap){ XXXXX    }   </script>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐