0

我正在尝试在我们的应用程序中实现 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);

彗星服务类

    @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);


}

我需要在服务器端代码中更改什么吗?

4

1 回答 1

1

您已经使您的频道变得懒惰,因此预计消息广播会延迟(这就是懒惰频道的全部内容)。

请查看惰性通道的文档

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

于 2013-10-16T13:01:36.190 回答