DWR3.0可以通过
1
2
|
//得到所有ScriptSession Collection<ScriptSession> sessions = Browser.getTargetSessions(); |
1
|
Collection pages = webContext.getScriptSessionsByPage( "/yourPage.jsp" ); |
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
|
//得到所有ScriptSession Collection<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的维护及优化
原文:http://www.cnblogs.com/-blog/p/5180980.html