CometD花了更多时间推送消息

时间:2013-10-16 11:42:29

标签: javascript cometd

我正在尝试在我们的应用程序中实现CometD。但与我们项目中的现有实施相比,它花费的时间更多。现有系统需要花费时间(以毫秒为单位),因为CometD需要2秒钟来推送消息。

我不确定我哪里出错了。任何指导都会对我有所帮助。

我的代码:

客户端的Java脚本

  (function($)
{
var cometd = $.cometd;

$(document).ready(function()
{
    function _connectionEstablished()
    {
        $('#body').append('<div>CometD Connection Established</div>');
    }

    function _connectionBroken()
    {
        $('#body').append('<div>CometD Connection Broken</div>');
    }

    function _connectionClosed()
    {
        $('#body').append('<div>CometD Connection Closed</div>');
    }

    // Function that manages the connection status with the Bayeux server
    var _connected = false;
    function _metaConnect(message)
    {
        if (cometd.isDisconnected())
        {
            _connected = false;
            _connectionClosed();
            return;
        }

        var wasConnected = _connected;
        _connected = message.successful === true;
        if (!wasConnected && _connected)
        {
            _connectionEstablished();
        }
        else if (wasConnected && !_connected)
        {
            _connectionBroken();
        }
    }

    // Function invoked when first contacting the server and
    // when the server has lost the state of this client
    function _metaHandshake(handshake)
    {
        if (handshake.successful === true)
        {
            cometd.batch(function()
            {
                cometd.subscribe('/java/test', function(message)
                {
                    $('#body').append('<div>Server Says: ' + message.data.eventID + ':'+ message.data.updatedDate + '</div>');
                });

            });
        }
    }

    // Disconnect when the page unloads
    $(window).unload(function()
    {
        cometd.disconnect(true);
    });

    var cometURL = "http://localhost:8080/cometd2/cometd";
    cometd.configure({
        url: cometURL,
        logLevel: 'debug'
    });

    cometd.addListener('/meta/handshake', _metaHandshake);
    cometd.addListener('/meta/connect', _metaConnect);

    cometd.handshake();
});
})(jQuery);

Comet服务类

    @Listener("/service/java/*")
    public void processMsgFromJava(ServerSession remote, ServerMessage.Mutable message)
    {

    Map<String, Object> input = message.getDataAsMap();
    String eventId = (String)input.get("eventID");
    //setting msg id

   String channelName = "/java/test";
    // Initialize the channel, making it persistent and lazy
    bayeux.createIfAbsent(channelName, new ConfigurableServerChannel.Initializer()
    {
        public void configureChannel(ConfigurableServerChannel channel)
        {
            channel.setPersistent(true);
            channel.setLazy(true);
        }
    });

    // Publish to all subscribers
    ServerChannel channel = bayeux.getChannel(channelName);
    channel.publish(serverSession, input, null);


}

我是否需要在服务器端代码中进行更改。

1 个答案:

答案 0 :(得分:1)

你的频道很懒,所以预计会延迟播放消息(这就是懒人频道的全部内容)。

请查看documentation for lazy channels

如果您想立即广播,请不要将频道设置为懒惰。