Wednesday, February 16, 2011

Wicket push reload support

When I look the cometd's instruction page here, it seems the the based source code do not support reload. As shown in the example, it requires a client side javascript code. But it was not written anywhere, as far as I saw.

I start supporting reload extension on my custom wicketstuff-push (see the post). Let's see the code.

public abstract class CometdAbstractBehavior extends AbstractDefaultAjaxBehavior {
    private static final long serialVersionUID = 1L;
 
    // FIXME: put this in application scope, we may have several webapp using
    // CometdBehavior in the same web container!
    private final static String cometdServletPath = getCometdServletPath();
  
    private static final ResourceReference COMETD =
        new CompressedResourceReference(CometdAbstractBehavior.class, "org/cometd.js");  
    private static final ResourceReference JQ_JSON2 =
        new CompressedResourceReference(CometdAbstractBehavior.class, "jquery/json2.js");
    private static final ResourceReference JQ_COMETD =
        new CompressedResourceReference(CometdAbstractBehavior.class, "jquery/jquery.cometd.js");

    private static final ResourceReference COMETD_RELOAD =
        new CompressedResourceReference(CometdAbstractBehavior.class, "org/cometd/ReloadExtension.js");
    private static final ResourceReference JQ_COOKIE =
        new CompressedResourceReference(CometdAbstractBehavior.class, "jquery/jquery.cookie.js");
    private static final ResourceReference JQ_COMETD_RELOAD =
        new CompressedResourceReference(CometdAbstractBehavior.class, "jquery/jquery.cometd-reload.js");

    @Override
    public void renderHead(final IHeaderResponse response) {
        super.renderHead(response);
        if (channelId == null) {
            throw new IllegalArgumentException("ChannelId in a CometdBehavior can not be null");
        }
        response.renderJavascriptReference(COMETD);
        response.renderJavascriptReference(JQ_JSON2);
        response.renderJavascriptReference(JQ_COMETD);

        response.renderJavascriptReference(COMETD_RELOAD);
        response.renderJavascriptReference(JQ_COOKIE);
        response.renderJavascriptReference(JQ_COMETD_RELOAD);
 
        response.renderJavascript(getInitCometdScript(), "initCometd");
        final String cometdInterceptorScript = getCometdInterceptorScript();
        if (cometdInterceptorScript != null) {
            response.renderJavascript(cometdInterceptorScript, "Interceptor"
                    + getBehaviorMarkupId());
        }
        response.renderJavascript(getSubscriberScript(), "Subscribe"
                + getBehaviorMarkupId());
    }

    protected final CharSequence getInitCometdScript() {
        return new PackagedTextTemplate(CometdBehavior.class, "CometdReloadInit.js").getString() +
            getConfigureCometdScript() + getHandshakeCometdScript(); 
    }

I add a javascript file. This code is written based on the reload example for jQuery (here).
/* handshake listener to report client IDs */
$.cometd.addListener("/meta/handshake", function(message)
{
    if (message.successful)
    {
        $('#previous').html(org.cometd.COOKIE.get('demoLastCometdID'));
        $('#current').html(message.clientId);
        org.cometd.COOKIE.set('demoLastCometdID', message.clientId, {
            'max-age': 300,
            path : '/',
            expires: new Date(new Date().getTime() + 300 * 1000)
        });
    }
    else
    {
        $('#previous').html('Handshake Failed');
        $('#current').html('Handshake Failed');
    }
});

/* Setup reload extension */
$(window).unload(function()
{
    $.cometd.reload();
});


Be care that this works on not only browser reloads, but also loading the same page with different parameters. It means, subscribing to the different channel also happens without handshake.

1 comment:

  1. I was actually looking for this resource a few weeks back. Thanks for sharing with us your wisdom.This will absolutely going to help me in my projects .
    Once again great post. You seem to have a good understanding of these themes.When I entering your blog,I felt this . Come on and keep writting your blog will be more attractive. To Your Success!
    Classic Dresses
    Classic Bridesmaid Dresses
    Trumpet Wedding Dresses
    New Style Flower Girl Dresses
    Wedding Dresses with Sleeves

    ReplyDelete