dwr消息推送---向指定用戶推送

頁面消息推送:

第一步:導入需要的兩個Jar包:(下載地址:http://directwebremoting.org/dwr/downloads/index.html

· dwr.jar

· commons-logging-1.1.1.jar

第二步:web.xml文件中添加 (目錄在WebContent/WEB-INF/lib/下)

  <servlet>
  	<servlet-name>dwr-invoker</servlet-name>
  	<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>

     <!-- 指定DWR核心Servlet處於調試狀態 -->
      <init-param>
          <param-name>debug</param-name>
          <param-value>true</param-value>
      </init-param>
      <!-- 設置使用反向Ajax技術 -->
      <init-param>
          <param-name>activeReverseAjaxEnabled</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>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>initApplicationScopeCreatorsAtStartup</param-name>  
           <param-value>true</param-value>  
        </init-param> 
         <init-param>  
            <param-name>logLevel</param-name>  
            <param-value>WARN</param-value>  
        </init-param>
  </servlet>
  
  
  <servlet-mapping>
  	<servlet-name>dwr-invoker</servlet-name>
  	<url-pattern>/dwr/*</url-pattern>
  </servlet-mapping>

第三步:創建dwr.xml文件 (放在與web.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.videocon.util.MessagePush"/>  
         </create>  
          <create creator="new" javascript="TestPush">  
            <param name="class" value="com.videocon.util.Test"/>  
         </create>  
     </allow>  
</dwr>  

第四步:根據dwr.xml文件暴露出的class,寫具體的類

MessagePush.java

public class MessagePush{
   public void onPageLoad(String userName) {  
  
       ScriptSession scriptSession = WebContextFactory.get().getScriptSession();  
  
       scriptSession.setAttribute(userName, userName);  
  
       DwrScriptSessionManagerUtil dwrScriptSessionManagerUtil = new DwrScriptSessionManagerUtil();  

       try {  
  
              dwrScriptSessionManagerUtil.init();  
              System.out.println("cacaca");  
  
       } catch (ServletException e) {  
  
              e.printStackTrace();  
  
       }  
   }
}
Test.java

public class Test{  
    public static void sendMessageAuto(String username, String message){  
          
        final String userName = username;  
        final String autoMessage = message;  
        Browser.withAllSessionsFiltered(new ScriptSessionFilter() {  
            public boolean match(ScriptSession session){  
            	System.out.println(session.getAttribute("userName"));
                if (session.getAttribute("userName") == null)  
                    return false;  
                else  
                    return (session.getAttribute("userName")).equals(userName);  
            }  
        }, new Runnable(){  
              
            private ScriptBuffer script = new ScriptBuffer();  
              
            public void run(){  
                  System.out.println("進入方法。。。");
                  script.appendCall("showMessage",autoMessage);  
                  
                Collection<ScriptSession> sessions = Browser.getTargetSessions();  
                for (ScriptSession scriptSession : sessions){  
                    scriptSession.addScript(script);  
                }  
            }  
        });  
    }  
 }  
還要添加一個工具類DwrScriptSessionManagerUtil.java

public class DwrScriptSessionManagerUtil extends DwrServlet{  
	  
    private static final long serialVersionUID = -7504612622407420071L;  
  
    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 userName =((PersonInfo) session.getAttribute("person")).getUsername()+"";
                         System.out.println("a ScriptSession is created!");  
                         ev.getSession().setAttribute("userName", userName);  
                  }  
                  public void sessionDestroyed(ScriptSessionEvent ev) {  
                         System.out.println("a ScriptSession is distroyed");  
                  }  
           };  
           manager.addScriptSessionListener(listener);  
    } 
}

第五步:寫推送端的界面:

具體我就不寫了,這裏只寫跟推送有關的jsp頁面代碼。

1.先引入js,engine.js 文件 是dwr的引擎文件,util.js 是dwr自帶的工具js,TestPush.js就是你在dwr.xml文件中配置的javaScript的別名,這裏只需要引入即可.

<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/TestPush.js"></script>
2.頁面js代碼

 $("#button").click(function(){
        test();	
});
     	  
function test() {  
      var msg = $("#username").val(); 
      console.log("推送中。。。"+msg);
      TestPush.sendMessageAuto(msg,"哈哈哈");  
              
 }  

第六步:寫接收端的界面

1.先引入js,engine.js 文件 是dwr的引擎文件,util.js 是dwr自帶的工具js,MessagePush.js就是你在dwr.xml文件中配置的javaScript的別名,這裏只需要引入即可.

<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>
2.頁面代碼

 $(document).ready(function(){
	onPageLoad();
	dwr.engine.setActiveReverseAjax(true);
	dwr.engine.setNotifyServerOnPageUnload(true);
		
});
     	  
//通過該方法與後臺交互,確保推送時能找到指定用戶  
function onPageLoad(){ 
  
	 var xx = 'username';
	 MessagePush.onPageLoad(xx);  

}  
 //推送信息  
function showMessage(autoMessage){  
    alert(autoMessage);//獲取推送信息
      
                     
} 






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章