DWR3.0框架入门(3) —— ScriptSession的维护及优化
当我们想向所有的页面访问者推送的时候,我们只需要,取得所有的页面访问者,就可以“推”了。
如何取得所有的页面访问者?
DWR3.0可以通过
|
1
2
|
//得到所有ScriptSessionCollection<ScriptSession> sessions = Browser.getTargetSessions(); |
|
1
|
Collection pages = webContext.getScriptSessionsByPage("/yourPage.jsp"); |
(5) 在上面的推送中产生的问题
上面的方法已经可以实现向所有的访问者推送。但是问题是,在客户端,如果用户刷新一次或多次,那么,Collection里面可能就保存了很多的无用的ScriptSession,所以不仅仅会影响性能问题,更重要的是,可能就不能实现你想要的功能。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
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(); }} |
|
1
2
3
4
5
6
7
8
9
10
11
|
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"); }} |
|
1
2
3
4
|
<init-param> <param-name >org.directwebremoting.extend.ScriptSessionManager </param-name> <param-value >sugar.dwr.DWRScriptSessionManager </param-value></init-param> |
|
1
2
|
//得到所有ScriptSessionCollection<ScriptSession> sessions = DWRScriptSessionListener.getScriptSessions(); |
|
1
2
|
//执行推送Browser.withAllSessionsFiltered(filter, run); //注意这里调用了有filter功能的方法 |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
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功能的方法} |
|
1
2
3
4
5
6
|
public void onPageLoad(final String tag){ //获取当前的ScriptSession ScriptSession scriptSession = WebContextFactory.get().getScriptSession(); scriptSession.setAttribute( "tag", tag); System. out.println( "setAttribute");} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<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 > |
DWR3.0框架入门(3) —— ScriptSession的维护及优化的更多相关文章
- DWR3.0框架入门(2) —— DWR的服务器推送
DWR3.0框架入门(2) —— DWR的服务器推送 DWR 在开始本节内容之前,先来了解一下什么是服务器推送技术和DWR的推送方式. 1.服务器推送技术和DWR的推送方式 传统模式的 Web ...
- DWR3.0框架入门(1) —— 实现ajax
框架简介:DWR(Direct Web Remoting) 是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开发人员开发包含AJAX技术的网站.它可以允许在浏 ...
- Spring框架入门之Spring4.0新特性——泛型注入
Spring框架入门之Spring4.0新特性——泛型注入 一.为了更加快捷的开发,为了更少的配置,特别是针对 Web 环境的开发,从 Spring 4.0 之后,Spring 引入了 泛型依赖注入. ...
- CodeIgniter框架入门教程——第一课 Hello World!
本文转载自:http://www.softeng.cn/?p=45 今天开始,我将在这里连载由我自己编写的<CodeIgniter框架入门教程>,首先,这篇教程的读着应该是有PHP基础的编 ...
- 【原创】NIO框架入门(四):Android与MINA2、Netty4的跨平台UDP双向通信实战
概述 本文演示的是一个Android客户端程序,通过UDP协议与两个典型的NIO框架服务端,实现跨平台双向通信的完整Demo. 当前由于NIO框架的流行,使得开发大并发.高性能的互联网服务端成为可能. ...
- 【原创】NIO框架入门(三):iOS与MINA2、Netty4的跨平台UDP双向通信实战
前言 本文将演示一个iOS客户端程序,通过UDP协议与两个典型的NIO框架服务端,实现跨平台双向通信的完整Demo.服务端将分别用MINA2和Netty4进行实现,而通信时服务端你只需选其一就行了.同 ...
- 【原创】NIO框架入门(二):服务端基于MINA2的UDP双向通信Demo演示
前言 NIO框架的流行,使得开发大并发.高性能的互联网服务端成为可能.这其中最流行的无非就是MINA和Netty了,MINA目前的主要版本是MINA2.而Netty的主要版本是Netty3和Netty ...
- 【原创】NIO框架入门(一):服务端基于Netty4的UDP双向通信Demo演示
申明:本文由作者基于日常实践整理,希望对初次接触MINA.Netty的人有所启发.如需与作者交流,见文签名,互相学习. 学习交流 更多学习资料:点此进入 推荐 移动端即时通讯交流: 215891622 ...
- yii2.0框架安装心得
yii2.0安装心得 能够搜索到这篇文章的朋友相信是对yii框架有兴趣的,但是我不得不吐槽的是,这个安装过程确实让人头疼,接下来就让大家见证一下这个纠结的过程 根据官网的说法,安装这个框架需要用到co ...
随机推荐
- 更换arm-linux-gcc 4.3.2编译器
先创建一个临时目录:mcx@mcx-virtual-machine:/home/work/tools$ mkdir tmp 解压到根目录:mcx@mcx-virtual-machine:/home/w ...
- Java 垃圾回收机制学习
原文链接: http://blog.csdn.net/zsuguangh/article/details/6429592 自己学习总结: 1c++和java的内存使用的区别: 在C++中,对象所占的内 ...
- OpenCv的Java,C++开发环境配置
1.OpenCV 下载及安装配置 opencv的下载地址:http://opencv.org/downloads.html 最新版本:opencv3.0.0 注意:支持的visual studio20 ...
- AndroidGradle--瘦身apk(转发)
apk瘦身一般有两条线, 去除无用的代码,例如引用一个比较大的lib,只使用了其中很少的功能.其他无用的代码可以想办法去掉 去除无用的资源文件,可能是第三方lib中的,也有可能是开发中引入了无用的资源 ...
- Oracle新建用户赋只读某几张表的权限
create user JSETI_WZQ identified by abcdef; -- 假设abcdef是密码 grant connect,resource to JSETI_WZQ; gran ...
- 使用for循环还是foreach循环?
很多时候我们很自然的认为,for循环的时候使用foreach和原来的for循环用下标的方式遍历是相同的. 而且因为foreach循环写法简单,很容易理解,而且少去了很多麻烦的变量,所以估计在学会使用f ...
- Memcached函数整理
public bool Memcached::add ( string $key , mixed $value [, int $expiration ] ) 向key中添加值,如果key存在,返回f ...
- java几种常用设计模式简单示例
1.单例设计模式 所谓单例设计模式简单说就是无论程序如何运行,采用单例设计模式的类(Singleton类)永远只会有一个实例化对象产生.具体实现步骤如下: (1) 将采用单例设计模式的类的构造方法私有 ...
- public private proteccted区别
public公共,加上这个修饰的类或属性,可以在同一个包或者别的包里面访问 private私有的,加上这个修饰的类或属性,只能在同类里访问,同包和别的包不能访问 protected保护,加上这个修饰的 ...
- 【抽屉定理】 组合数学poj2356
假定n个数为a1,a2,...,an,前n项和分别是S1.S2.....Sn,那么如果有一个Si模n是0,就是答案,否则,n个数模n的余数只能在 1到n - 1之间,把余数作为抽屉,显然n个数放到n ...