dwr的Reverse Ajax,新瓶装老酒,其实就是comet技术,即服务端向客户端push js代码,典型的应用场景就是目前比较火的比赛图文直播,还有聊天室等,消息订阅模式的一种实现。
优点:控制权反转,打开一个网页(或进行一次操作)后,订阅了一个消息,一个客户端服务端连接将维持,服务端检测到数据更新,将自动push数据到客户端,这样服务端对于程序调动的灵活性更加大,避免了客户端大量的循环请求数据。
缺点:连接维持,访问数量一大连接数将很快饱和。
关于comet优点比较权威的阐述:Ajax improves single-user responsiveness. Comet improves application responsiveness for collaborative, multi-user applications and does it without the performance headaches associated with intermittent polling.
看一下dwr官方示例,很简单的clock实现:
html代码:
- <input type="button" value="Start / Stop" onclick="Clock.toggle();"/> <h2 id="clockDisplay"></h2>
<input type="button" value="Start / Stop" onclick="Clock.toggle();"/> <h2 id="clockDisplay"></h2>
js代码就一行,打开相应属性:
dwr.engine.setActiveReverseAjax(true);
服务端clock类:
-
- public class Clock implements Runnable {
- public Clock() {
- ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
- executor.scheduleAtFixedRate(this, 1, 1, TimeUnit.SECONDS);
- }
-
- public void run() {
- if (active) {
- setClockDisplay(new Date().toString());
- }
- }
-
-
-
-
- public synchronized void toggle() {
- active = !active;
-
- if (active) {
- setClockDisplay("Started");
- }
- else {
- setClockDisplay("Stopped");
- }
- }
-
-
-
-
-
- public void setClockDisplay(final String output) {
- String page = ServerContextFactory.get().getContextPath() + "/reverseajax/clock.html";
- Browser.withPage(page, new Runnable() {
- public void run() {
- Util.setValue("clockDisplay", output);
- }
- });
- }
-
- protected transient boolean active = false;
- }
public class Clock implements Runnable {
public Clock() {
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
executor.scheduleAtFixedRate(this, 1, 1, TimeUnit.SECONDS);
}
public void run() {
if (active) {
setClockDisplay(new Date().toString());
}
}
/**
* Called from the client to turn the clock on/off
*/
public synchronized void toggle() {
active = !active;
if (active) {
setClockDisplay("Started");
}
else {
setClockDisplay("Stopped");
}
}
/**
* Actually alter the clients.
* @param output The string to display.
*/
public void setClockDisplay(final String output) {
String page = ServerContextFactory.get().getContextPath() + "/reverseajax/clock.html";
Browser.withPage(page, new Runnable() {
public void run() {
Util.setValue("clockDisplay", output);
}
});
}
protected transient boolean active = false;
}
个人设想:这个特性可以用来远程实时监控服务器运行状态,服务端自定义调用接口,定义一套xml协议来实时传送数据,也可以利用Web Service。
分享到:
相关推荐
网络上Spring整合DWR comet 实现无刷新、多人聊天室 的代码整理,包含jar包,eclipse工程,直接导入即可运行。
既然找到了这个,我就不必多解释了,下下来可以直接运行
NULL 博文链接:https://happysoul.iteye.com/blog/1313069
dwr comet 交互方式,使用dwr3.0+spring2.5
实现Comet消息推送功能,根据登陆人定向推送,解决刷新页面原有ScriptSession不能及时销毁的问题,DEMO比较简陋,请先进入login.jsp页面登陆。根据登陆名称判断推送目标,可登陆多个用户进行测试。
场景:页面comet.jsp接受服务器推送的信息并显示,页面action.jsp执行一个动作,调用 DwrServer.perform方法,perform方法做某些事,并发送事件信息PerformInfo。NotifyClient监听事件,当 接收到PerformInfo后,把...
使用dwr的comet技术来实现无刷新的多人聊天室。让服务端监听发送消息事件,通过scriptsession来获取所有的访问者。在像访问者中推送信息。
NULL 博文链接:https://frank59.iteye.com/blog/1455050
DWR学习资料 :DWR 3.0 上传文件.txt DWR3.0反向Ajax示例.txt DWR3.0学习笔记.txt DWR3.0学习网址.txt dwr分页.doc DWR分页代码.doc DWR中文文档.doc DWR中文文档.pdf dwr做comet的完整实现.doc Spring整合DWR comet ...
dwr comet web push 服务器主动推送
使用 DWR+Spring 实现服务器推送的 网页聊天程序
DWR comet 多人web聊天例子。
DWR+JAVA进行web消息推送dwr-comet.zip
dwr的comet技术实现即时通讯的DEMO
spring整合dwr 完成无刷新的ajax聊天室功能 利用comet技术
dwr笔记 dwr自学资料 dwr笔记 dwr自学资料 dwr笔记 dwr自学资料
dwr comet 反向ajax实力 直接抛 我打了一个包, 放到Tomcat,jetty下面就能直接跑了 很方便 还有注视 对新手 。。。。很好的
comet.jsp就是聊天室接收消息的界面,action.jsp就是发送消息的界面,为了更好的说明问题,将接收界面和发送界面放在两个窗口中, 测试时同时可以打开多个接收界面和发送界面, 执行发送界面的发送按钮会发现在所有...
dwr与spring整合的相关资料收集dwr与spring整合的相关资料收集dwr与spring整合的相关资料收集dwr与spring整合的相关资料收集
实现Comet消息推送功能,根据登陆人定向推送,解决刷新页面原有ScriptSession不能及时销毁的问题,DEMO比较简陋,请先进入login.jsp页面登陆。根据登陆名称判断推送目标,可登陆多个用户进行测试。