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>

相关文章

IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...