<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd"> <dwr> <allow> <create creator="new" javascript="PushMessage"> <param name="class" value="com.manhui.push.controller.PushMessage"/> </create> </allow> </dwr>
關於dwr的介紹可自行度娘,這裏主要作一個dwr進行消息推送的demojavascript
第一步:這裏使用maven項目,先引入jar的倉庫地址:html
1 <dependency> 2 <groupId>org.directwebremoting</groupId> 3 <artifactId>dwr</artifactId> 4 <version>3.0.0-RELEASE</version> 5 </dependency>
第二步:web.xml配置java
<!-- dwr相關配置 --> <servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>logLevel</param-name> <param-value>WARN</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>activeReverseAjaxEnabled</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>maxWaitAfterWrite</param-name> <param-value>6000</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
第三步:後臺編碼web
package com.manhui.push.controller; import java.util.Collection; import org.apache.log4j.Logger; import org.directwebremoting.Browser; import org.directwebremoting.ScriptBuffer; import org.directwebremoting.ScriptSession; import org.directwebremoting.ScriptSessionFilter; import org.directwebremoting.WebContextFactory; /** * 推送消息控制層,這裏主要是進行消息的精確推送,固然若是要進行全部推送,則不須要進行過濾處理便可 * @author hq * */ public class PushMessage { private Logger log = Logger.getLogger(PushMessage.class); // 載入頁面時調用,傳入name值做爲推送的標識 public void onPageLoad(String name) { ScriptSession session = WebContextFactory.get().getScriptSession(); session.setAttribute("name", name); } public void addMessage(String userid, String message) { final String userId = userid; final String autoMessage = message; ScriptSession session = WebContextFactory.get().getScriptSession(); String from =(String) session.getAttribute("name"); // 獲取全部scriptsession並經過ScriptSessionFilter篩選符合條件的ScriptSession Browser.withAllSessionsFiltered(new ScriptSessionFilter() { // 實現match方法,條件爲真爲篩選出來的session public boolean match(ScriptSession session) { String name = session.getAttribute("name").toString(); return name == null ? false : userId.equals(name); } }, new Runnable() { private ScriptBuffer script = new ScriptBuffer(); public void run() { // 設定前臺接受消息的方法和參數 script.appendCall("receiveMessages", autoMessage); Collection<ScriptSession> sessions = Browser .getTargetSessions();//獲取有效的scriptsession // 向全部符合條件的頁面推送消息 for (ScriptSession scriptSession : sessions) { if (scriptSession.getAttribute("name").equals(userId)) { scriptSession.addScript(script); } } } }); } }
第四步:在web.xml同級目錄下建立dwr.xml文件apache
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd"> <dwr> <allow> <create creator="new" javascript="PushMessage"> <param name="class" value="com.manhui.push.controller.PushMessage"/> </create> </allow> </dwr>
第五步:在須要推送消息的頁面引入相關js,並在頁面初始化以前加載相關方法session
<script type="text/javascript" src="dwr/engine.js"></script> <script type="text/javascript" src="dwr/util.js"></script> <script type="text/javascript" src="dwr/interface/PushMessage.js"></script>
<script type="text/javascript" src="dwr/interface/PushMessage.js"></script>該js文件是根據dwr.xml自行生成的,前面的是dwr.jar裏面的app
頁面初始化加載調用:maven
dwr.engine.setActiveReverseAjax(true);// 開啓逆向Ajax,也可寫在body標籤的onload方法中 dwr.engine.setNotifyServerOnPageUnload(true); dwr.engine.setErrorHandler(function(){});//" 這個方法 防止項目已經關閉,客戶頁面還未關閉,頁面會談Error的問題 onPageLoad();
var chatlog = ""; var name = '${name}'; function sendMessage() { var message = $("#message").val(); var user = $("#user").val(); // 經過代理調用後臺的addMessage方法發送消息 PushMessage.addMessage(user, message); } // 前臺接受消息的方法,由後臺調用 function receiveMessages(messages) { var lastMessage = messages; chatlog = "<p>" + lastMessage + "</p>" + chatlog; $("#list").html(chatlog); } //讀取name值做爲推送的惟一標示 function onPageLoad(){ // 獲取URL中的name屬性爲惟一標識符 var userId = name; $("#myName").html(userId); // 經過代理,傳入區別本頁面的惟一標識符 PushMessage.onPageLoad(userId); }
第六步:效果以下ide