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 ...
随机推荐
- vbs 截图
'VBS截屏.vbs ' Win7x64 测试通过(已安装Word2007): '参考: ' http://qtp.blogspot.com/2010/02/screenshot-vbscript ...
- drawable文件夹详解
QVGA使用ldpi,虽然有不同尺寸,但都是120dpi左右:HVGA同理:如下图: -finger 用于触摸屏的设备 -hdpi 近似于240dpi的高级显示密度的屏幕 -mdpi ...
- Centos下安装jdk详解
环境: 系统: [root@Wulaoer ~]# cat /proc/version Linux version 2.6.32-431.el6.x86_64 (mockbuild@c6b8.bsys ...
- html5中拨打电话代码
<a href="tel:18600000000">给我打电话</a> <a href="sms:18600000000"&g ...
- Android5.0 Gallery2上编译Gallery模块出错
Android5.0 Gallery2上编译Gallery模块出错 时间:2015-05-05 19:32:57 阅读:105 评论:0 收藏:0 [点我收藏+ ...
- vmware中网络连接方式介绍
- PAT (Advanced Level) 1052. Linked List Sorting (25)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
- HDU 5512 Pagodas
2015 ACM / ICPC 沈阳现场赛 D 题 找了一小时规律......发现是个GCD. #include<cstdio> #include<cstring> #incl ...
- Allegro PCB -内层分割,比如电源层需要分割几种电源
内层分割,比如电源层需要分割几种电源. (1).点击Display -> Assign Color 在Option中,先取一种颜色作为高亮显示的颜色. (2).在Find中,选Net,点击mor ...
- PS2鼠标+LCD12864实验——终于OK 了
抱着“不气馁.不放弃.誓不罢休.搞不定你我还能搞其他玩意吗”的心态,调试许久的PS2鼠标实验,终于在今天被我搞定了.发几张图显摆一下,嘿嘿... 左键按下+鼠标移动 右键按下+鼠标移动 中键按 ...