1.引jar包,同上一篇。
<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)//接收消息 }