原文出处:http://www.kankanews.com/ICkengine/archives/82552.shtmljavascript
在DWR中,咱们能够经过WebContextFactory.get()来取得一个WebContext对象,进而经过WebContext的getScriptSession()取得ScriptSession对象。html
可是要注意,在咱们自定义的Servlet中,咱们也能够经过WebContextFactory.get()来取得一个WebContext,可是这种方法却不能取得ScriptSession对象。由于,此WebContext对象其实不是经过DWR的上下文环境获得的,因此,就根本没有建立ScriptSession对象。java
假设这种方式也能获得ScriptSession的话,那么咱们实现“推”也就能够不局限在DWR的上下文环境中了,那么其灵活性就会大不少了。因此,这就是咱们不能在Servlet中实现推的缘由。web
在咱们须要推送的页面中,若是你刷新一下,那么就提交一个Http的request,此时,若是是第一次,那么就会建立一个httpSession对象,同时,请求由DwrServlet来处理后,就会建立一个ScriptSession.这个ScriptSession会和你的request请求的URI绑定放在一个由ScriptSessionManager维护的Map里面(这里面实际上是一个URI对应的Set,在Set里面放置的是URI绑定的全部ScriptSession)。spring
当你刷新的时候,一样的一个HttpSession,却会建立一个新的ScriptSession,而后绑定到对应的URI上。服务器
(4)向全部的页面访问者推送
当咱们想向全部的页面访问者推送的时候,咱们只须要,取得全部的页面访问者,就能够“推”了。
如何取得全部的页面访问者?session
DWR3.0能够经过mvc
//获得全部ScriptSession Collection<ScriptSession> sessions = Browser.getTargetSessions();
DWR2.x能够经过app
Collection pages = webContext.getScriptSessionsByPage("/yourPage.jsp");
(5) 在上面的推送中产生的问题
上面的方法已经能够实现向全部的访问者推送。可是问题是,在客户端,若是用户刷新一次或屡次,那么,Collection里面可能就保存了不少的无用的ScriptSession,因此不单单会影响性能问题,更重要的是,可能就不能实现你想要的功能。jsp
2.如何管理有效的ScriptSession
因为上面的问题,咱们就须要本身管理ScriptSession。其实,有效地HttpSession,就是那个和当前的HttpSession匹配的ScriptSession。
因此,咱们就能够本身维护一个Map,在这个Map里面,咱们定义key就是HttpSession的Id,其值就是ScriptSession对象。
在每一次页面载入的时候,都去注册此ScriptSession,那么就会把新的ScriptSession绑定到httpSession上面了。
在DWR3.0中推出了 ScriptSessionListener用来监听ScriptSession的建立及销毁事件。咱们能够使用该监听器来维护咱们本身的Map。
在上一节的代码上修改Demo:
1.新建一个类实现
ScriptSessionListener接口
package sugar.dwr; import java.util.Collection; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpSession; import org.directwebremoting.ScriptSession; import org.directwebremoting.WebContext; import org.directwebremoting.WebContextFactory; import org.directwebremoting.event.ScriptSessionEvent; import org.directwebremoting.event.ScriptSessionListener; public class DWRScriptSessionListener implements ScriptSessionListener { //维护一个Map key为session的Id, value为ScriptSession对象 public static final Map<String, ScriptSession> scriptSessionMap = new HashMap<String, ScriptSession>(); /** * ScriptSession建立事件 */ 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!"); } /** * ScriptSession销毁事件 */ 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!"); } /** * 获取全部ScriptSession */ public static Collection<ScriptSession> getScriptSessions(){ return scriptSessionMap.values(); } }
2.新建一个类继承
DefaultScriptSessionManager,用来绑定
DWRScriptSessionListener
package sugar.dwr; import org.directwebremoting.impl.DefaultScriptSessionManager; public class DWRScriptSessionManager extends DefaultScriptSessionManager { public DWRScriptSessionManager(){ //绑定一个ScriptSession增长销毁事件的监听器 this.addScriptSessionListener( new DWRScriptSessionListener()); System. out.println( "bind DWRScriptSessionListener"); } }
3.在web.xml中将
DWRScriptSessionManager
配置在
dwr-invoker servlet中
<init-param> <param-name >org.directwebremoting.extend.ScriptSessionManager </param-name> <param-value >sugar.dwr.DWRScriptSessionManager </param-value> </init-param>
这样在服务器启动时即会绑定
ScriptSessionListener,
ScriptSession在建立时会自动添加到咱们维护的Map中
补充:若是是使用Spirng MVC管理的,能够使用以下方法:
web.xml
<!-- spring mvc --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value></param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
spring-MVC.xml
<!-- 要求dwr在spring容器中检查拥有@RemoteProxy 和 @RemoteMethod注解的类。注意它不会去检查Spring容器以外的类。 --> <dwr:annotation-config id="dwr" /> <!-- 要求DWR将util.js和engine.js映射到dwrController --> <dwr:url-mapping /> <!-- 定义dwr --> <dwr:controller id="dwrController" debug="true"> <dwr:config-param name="activeReverseAjaxEnabled" value="true" /> <dwr:config-param name="allowScriptTagRemoting" value="true" /> <dwr:config-param name="crossDomainSessionSecurity" value="false" /> <dwr:config-param name="Async" value="false" /> <dwr:config-param name="scriptSessionTimeout" value="1800000"/> </dwr:controller> <dwr:configuration> <dwr:convert type="bean" class="com.grandthink.ms.vo.RuleFilterRecordVO"></dwr:convert> </dwr:configuration>
记得导入命名空间:
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd
如下为ScriptSessionListener的配置:
@Service public class DWRScriptSessionManager extends DefaultScriptSessionManager { public DWRScriptSessionManager() { // 绑定一个ScriptSession增长销毁事件的监听器 this.addScriptSessionListener(new DWRScriptSessionListener()); System.out.println("bind DWRScriptSessionListener"); } }
4.经过如下方法获取全部的
ScriptSession
//获得全部ScriptSession Collection<ScriptSession> sessions = DWRScriptSessionListener.getScriptSessions();
3.使用
ScriptSessionFilter过滤
若是咱们不想要给全部的客户端
推送消息,只想给特定的客户端推送,那么咱们能够使用
ScriptSessionFilter来实现。在filter中去断定session中的Attribute值是否是咱们给定的。
1.使用如下方法推送消息
//执行推送 Browser.withAllSessionsFiltered(filter, run); //注意这里调用了有filter功能的方法
2.经过参数可知咱们须要一个
ScriptSessionFilter对象,此时send的方法改写成以下:
public void send(final String content){ //过滤器 ScriptSessionFilter filter = new ScriptSessionFilter() { public boolean match(ScriptSession scriptSession) { String tag = (String)scriptSession.getAttribute("tag" ); System. out.println(tag); return "receiverTag" .equals(tag); } }; Runnable run = new Runnable(){ private ScriptBuffer script = new ScriptBuffer(); public void run() { //设置要调用的 js及参数 script.appendCall( "show", content); //获得全部ScriptSession Collection<ScriptSession> sessions = DWRScriptSessionListener.getScriptSessions(); //遍历每个ScriptSession for (ScriptSession scriptSession : sessions){ scriptSession.addScript( script); } } }; //执行推送 Browser. withAllSessionsFiltered(filter, run); //注意这里调用了有filter功能的方法 }
3.在打开jsp页面时须要在
ScriptSession
中注入设定的attribute,MessagePush中的方法
public void onPageLoad(final String tag){ //获取当前的ScriptSession ScriptSession scriptSession = WebContextFactory.get().getScriptSession(); scriptSession.setAttribute( "tag", tag); System. out.println( "setAttribute"); }
4.在jsp中调用该方法
<script type= "text/javascript"> //这个方法用来启动该页面的ReverseAjax功能 dwr.engine.setActiveReverseAjax( true); //设置在页面关闭时,通知服务端销毁会话 dwr.engine.setNotifyServerOnPageUnload( true); var tag = "receiverTag"; //自定义一个标签 messagePush.onPageLoad(tag); //这个函数是提供给后台推送的时候 调用的 function show(content){ $( "#content").text(content); } </script >
这样咱们能够给不一样客户端的jsp中导入不一样的tag值,过滤推送的客户端