1

我正在尝试开始使用 CometD,因为我需要一些经验背景来对各种推送技术进行技术比较。

我终于设法在我的 Tomcat (v7.0) 中运行了一个示例代码。

现在我有奇怪的行为,我拼命想找出我做错了什么。

以下是我的观察:

  • 连接循环:

    我在与客户端的无限连接循环中运行。

    要求:

    [{"channel":"/meta/connect","connectionType":"long-polling","id":"19","clientId":"f1le33y91f6pa71f39z52km87yp"}]
    

    回复:

    [{"id":"19","successful":true,"advice":{"interval":2000,"reconnect":"retry","multiple-clients":true,"timeout":60000},"channel":"/meta/connect"}]
    

    有趣的是,Listener-callback 事件实际上发生了

     cometd.addListener('/meta/handshake', _metaHandshake);
    
  • 客户端连接请求卡住

    这主要发生在我的 Tomcat 服务器重新启动之后。我可以看到成功订阅了 2 个频道:

    要求

    [{"version":"1.0","minimumVersion":"0.9","channel":"/meta/handshake","supportedConnectionTypes":["long-polling","callback-polling"],"advice":{"timeout":60000,"interval":0},"id":"1"}]
    

    回复

    [{"id":"1","minimumVersion":"1.0","supportedConnectionTypes":["callback-polling","long-polling"],"successful":true,"channel":"/meta/handshake","clientId":"11tnpjnmkqo3tf64zcvufuqbtv","version":"1.0"}]
    

    要求

    [{"channel":"/meta/subscribe","subscription":"/hello","id":"2","clientId":"11tnpjnmkqo3tf64zcvufuqbtv"},{"channel":"/service/hello","data":{"name":"World"},"id":"3","clientId":"11tnpjnmkqo3tf64zcvufuqbtv"}]
    

    回复

    [{"id":"2","subscription":"/hello","successful":true,"channel":"/meta/subscribe"},{"id":"3","successful":true,"channel":"/service/hello"}]
    

    这个之后的电话卡住并且永远需要。

... abd 这是我的代码

  • 客户

    (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)
            {
               // interesting enough: that event is triggered
                alert('event occured');
                cometd.batch(function()
                {
                    // that function is called and produces a corresponding request to the server
                    cometd.subscribe('/hello', function(message)
                    {
                        // that one never happens... 
                        $('#body').append('<div>Server Says: ' + message.data.greeting + '</div>');
                    });
                    // Publish on a service channel since the message is for the server only
                    cometd.publish('/service/hello', { name: 'World' });
                });
            }
        }
    
        // Disconnect when the page unloads
        $(window).unload(function()
        {
            cometd.disconnect(true);
        });
    
        var cometURL = location.protocol + "//" + location.host + config.contextPath + "/cometd";
        cometd.configure({
            url: cometURL,
            logLevel: 'debug'
        });
    
        cometd.addListener('/meta/handshake', _metaHandshake);
        cometd.addListener('/meta/connect', _metaConnect);
    
        cometd.handshake();
    });
    })(jQuery);
    
  • 服务器

    请注意,这与分发包中的示例几乎是 1:1 的!

    public class HelloService extends AbstractService
    {
    public HelloService(BayeuxServer bayeux)
    {
        super(bayeux, "hello");
        addService("/service/hello", "processHello");
    }
    
    public void processHello(ServerSession remote, Message message)
    {
        Map<String, Object> input = message.getDataAsMap();
        String name = (String)input.get("name");
    
        Map<String, Object> output = new HashMap<String, Object>();
        output.put("greeting", "Hello, " + name);
        remote.deliver(getServerSession(), "/hello", output, null);
    }
    

    }

  • Web.xml 文件

    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

    <servlet>
        <servlet-name>cometd</servlet-name>
        <servlet-class>org.cometd.server.CometdServlet</servlet-class>
        <async-supported>true</async-supported>
        <init-param>
            <param-name>timeout</param-name>
            <param-value>60000</param-value>
        </init-param>
        <init-param>
            <param-name>logLevel</param-name>
            <param-value>3</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>cometd</servlet-name>
        <url-pattern>/cometd/*</url-pattern>
    </servlet-mapping>
    

也许我只是在做一些完全错误的事情,但我开始对此感到有些绝望。帮助将不胜感激。

4

0 回答 0