dwr comet

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代码:

Html代码 复制代码
  1. <input type="button" value="Start / Stop" onclick="Clock.toggle();"/> <h2 id="clockDisplay"></h2>  
<input type="button" value="Start / Stop" οnclick="Clock.toggle();"/> <h2 id="clockDisplay"></h2>


js代码就一行,打开相应属性:
dwr.engine.setActiveReverseAjax(true);
服务端clock类:

Java代码 复制代码
  1.   
  2. public class Clock implements Runnable {   
  3.     public Clock() {   
  4.         ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);   
  5.         executor.scheduleAtFixedRate(this11, TimeUnit.SECONDS);   
  6.     }   
  7.   
  8.     public void run() {   
  9.         if (active) {   
  10.             setClockDisplay(new Date().toString());   
  11.         }   
  12.     }   
  13.   
  14.     /**  
  15.      * Called from the client to turn the clock on/off  
  16.      */  
  17.     public synchronized void toggle() {   
  18.         active = !active;   
  19.   
  20.         if (active) {   
  21.             setClockDisplay("Started");   
  22.         }   
  23.         else {   
  24.             setClockDisplay("Stopped");   
  25.         }   
  26.     }   
  27.   
  28.     /**  
  29.      * Actually alter the clients.  
  30.      * @param output The string to display.  
  31.      */  
  32.     public void setClockDisplay(final String output) {   
  33.         String page = ServerContextFactory.get().getContextPath() + "/reverseajax/clock.html";   
  34.         Browser.withPage(page, new Runnable() {   
  35.             public void run() {   
  36.                 Util.setValue("clockDisplay", output);   
  37.             }   
  38.         });   
  39.     }   
  40.   
  41.     protected transient boolean active = false;   
  42. }  
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。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值