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 ...
随机推荐
- C语言开发工具
1.编写程序的工具: indent命令将自动调整C代码的缩进风格,个人比较喜欢indent -kr 2.编译C语言程序: 1.gcc编译器: gcc是一个ANSI C兼容编译器,C++编译器也可以编译 ...
- acm课程练习2--1001
题目描述 Now,given the equation 8x^4 + 7x^3 + 2x^2 + 3x + 6 == Y,can you find its solution between 0 and ...
- [HDU] 2094 产生冠军(拓扑排序+map)
产生冠军 Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissi ...
- O(n)线性时间找第K大,中位数
运用快速排序的思想,可以达到线性时间找到一串数的第K大 #include<cstdio> #define F(i,a,b) for(int i=a;i<=b;i++) ],n; vo ...
- java工程开发之图形化界面之(第三课)
上面我们讲述了通过JOptionPane进行文本I/O操作,一个是通过JOptionPane来获取的 参考链接:http://blog.sina.com.cn/s/blog_993d254201013 ...
- awk 数组排序-- asort 与 asorti
两者排序区别: asort 是对数组的值进行排序,并且会丢掉原先键值: asorti是对数组的下标进行排序. 数据文件: 12 34 78 90 23 45 1. awk是关联数组.for-in循环输 ...
- 如何在Apache中配置多端口访问
环境: Windows server 2008 R2, Apache, PHP5 步骤: 建立一个目录,里面放置一个index.php. 打开Apache\conf\httpd.conf 文件,做如下 ...
- 草,又学了个新命令,nc传文件。
nc -l 5222 > aa nc 192.168.0.48 5222 < a http://www.linuxso.com/command/nc.html
- cell选中与取消选中调用的方法
//选中与取消选中都会调用哦,注意!!- (void)setSelected:(BOOL)selected animated:(BOOL)animated{ [super setSelected:se ...
- html5语义化标签总结二
HTML 5的革新之一:语义化标签二文本元素标签.分组元素标签. HTML 5的革新——语义化标签(一)中介绍了一些HTML5新加的一些节元素,一张页面中结构元素构成网页大体,但是也需要其他内容来填充 ...