`
123123ghiwjn
  • 浏览: 24677 次
  • 性别: Icon_minigender_2
  • 来自: 陕西
社区版块
存档分类
最新评论

dwr comet

    博客分类:
  • ajax
阅读更多

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" onclick="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。

分享到:
评论
3 楼 fantasyshe 2010-10-19  
没有代码下载,没真相
2 楼 high_java 2010-08-24  
sorry, 点错了
1 楼 high_java 2010-08-24  

相关推荐

Global site tag (gtag.js) - Google Analytics