dwr消息推送----向所有用户推送

dwr消息推送,向所有进入此页面用户推送。

1.引jar包,同上一篇

2.在web.xml文件添加

<listener>
    <listener-class>org.directwebremoting.servlet.DwrListener</listener-class>
  </listener>
  <servlet>
  <servlet-name>dwr-invoker</servlet-name>
        <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
    <init-param>
    <param-name>crossDomainSessionSecurity</param-name>
    <param-value>false</param-value>
   </init-param>
  <init-param>
    <param-name>allowScriptTagRemoting</param-name>
    <param-value>true</param-value>
   </init-param>
  <init-param>
    <param-name>classes</param-name>
    <param-value>java.lang.Object</param-value>
   </init-param>
  <init-param>
    <param-name>activeReverseAjaxEnabled</param-name>
    <param-value>true</param-value>
   </init-param>
   <init-param>
    <param-name>pollAndCometEnabled</param-name>
   <param-value>true</param-value>
   </init-param>
  <init-param>
    <param-name>initApplicationScopeCreatorsAtStartup</param-name>
    <param-value>true</param-value>
    </init-param>
        <init-param>
        <param-name>jsonpEnabled</param-name>
        <param-value>true</param-value>
     </init-param>
  <init-param>
    <param-name>maxWaitAfterWrite</param-name>
        <param-value>3000</param-value>
        </init-param>
        <init-param>
         <param-name >org.directwebremoting.extend.ScriptSessionManager</param-name>
         <param-value >com.qcmsa.util.DWRScriptSessionManager</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
   </servlet>
  <servlet-mapping>
    <servlet-name>dwr-invoker</servlet-name>
    <url-pattern>/dwr/*</url-pattern>
  </servlet-mapping>

3.创建dwr.xml文件
<!DOCTYPE dwr PUBLIC  
     "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN"  
     "http://getahead.org/dwr/dwr30.dtd"> 
<dwr>  
     <allow>  
          <create creator="new" javascript="MessagePush">  
            <param name="class" value="com.qcmsa.util.MessagePush"/>  
         </create>  
     </allow>  
</dwr> 
4.根据上面提到的写类的方法及dwr的监听事件

DWRScriptSessionManager.java

public class DWRScriptSessionManager extends DefaultScriptSessionManager{  
	  
    private static final long serialVersionUID = -7504612622407420071L;  
    
    public DWRScriptSessionManager(){
    	  //绑定一个ScriptSession增加销毁事件的监听器
    	  this.addScriptSessionListener(new DWRScriptSessionListener());
    	 }
}
DWRScriptSessionListener.java
public class DWRScriptSessionListener implements ScriptSessionListener {
	//维护一个Map key为session的Id, value为ScriptSession对象
    public static Map<String,ScriptSession> scriptSessionMap = new HashMap<String,ScriptSession>();

 @Override
 public void sessionCreated(ScriptSessionEvent event) {
  WebContext webContext = WebContextFactory. get();
  HttpSession session = webContext.getSession();
  ScriptSession scriptSession = event.getSession();
  scriptSessionMap.put(session.getId(),scriptSession);     //添加scriptSession
  System. out.println("[新增]session: " + session.getId() + "> scriptSession: " + scriptSession.getId() + "is created!");
 }

 @Override
 public void sessionDestroyed(ScriptSessionEvent event) {
  WebContext webContext = WebContextFactory. get();
  HttpSession session = webContext.getSession();
  ScriptSession scriptSession = scriptSessionMap.remove(session.getId());  //移除scriptSession
  System. out.println("[删除]session: " + session.getId() + " scriptSession: " + scriptSession.getId() + "is destroyed!");
 }
 public static Collection<ScriptSession> getScriptSessions(){
  return scriptSessionMap.values();
 }
}
MessagePush.java
public class MessagePush{
	 public static void getMessage(final String content) throws Exception {  
	        Runnable run = new Runnable() {  
	            private ScriptBuffer script = new ScriptBuffer();  
	            public void run() {  
	                // 设置要调用的 js及参数  
	                script.appendCall("showMessage",content);  
	                // 得到所有ScriptSession  
	                Collection<ScriptSession> sessions = browser  
	                        .getTargetSessions();  
	                // 遍历每一个ScriptSession  
	                for (ScriptSession scriptSession : sessions) {  
	                    scriptSession.addScript(script);  
	                }  
	            }  
	        };  
	        // 执行推送  
	        browser.withAllSessions(run);  
	    }  
}

5.页面推送

引js,同上

    <script type="text/javascript" src="${ctx!}/dwr/engine.js"></script>  
    <script type="text/javascript" src="${ctx!}/dwr/util.js"></script>  
    <script type="text/javascript" src="${ctx!}/dwr/interface/MessagePush.js"></script>  


  $(document).ready(function(){

	dwr.engine.setActiveReverseAjax(true);
	dwr.engine.setNotifyServerOnPageUnload(true);	
	MessagePush.getMessage("gdjsj");//推送消息
		     
});	       
	     
//推送信息  
 function showMessage(content){
	alert(content)//接收消息
 }

相关文章

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