最简单的可能工作的cometd java客户端

时间:2017-08-18 16:35:32

标签: java cometd

我一直试图使用cometd的文档中的代码,使用cometd版本3.1.2和jetty 9.4.6.v20170531来获得一个非常简单的cometd java服务器和客户端设置。

HttpClient httpClient = new HttpClient();
httpClient.start();

Map<String, Object> options = new HashMap<String, Object>();
ClientTransport transport = new LongPollingTransport(options, httpClient);
BayeuxClient client = new BayeuxClient("http://localhost:9025/mycontext/cometd/", transport);

client.handshake();
boolean handshaken = client.waitFor(5000, BayeuxClient.State.CONNECTED);
if (handshaken) {
  System.out.println("Yes");
  // Here handshake is successful
}
System.out.println("Done");

服务器设置是我真正感兴趣的,并且是带有cometd servlet的spring注入嵌入式jetty服务器。无论我尝试过什么,客户都不会握手。我终于能够通过邮递员发送此请求来证明服务器设置正确并且可用。

POST http://localhost:9025/mycontext/cometd

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

...返回200 ...

[
    {
        "minimumVersion": "1.0",
        "clientId": "11jlmrekgqnu9v1i4w1b8rgiq3c",
        "supportedConnectionTypes": [
            "long-polling",
            "websocket"
        ],
        "advice": {
            "interval": 0,
            "timeout": 60000,
            "reconnect": "retry"
        },
        "channel": "/meta/handshake",
        "id": "1",
        "version": "1.0",
        "successful": true
    }
]

...显然,服务器设置很好。我设置了断点并使用tcpflow来查找流量,据我所知,使用来自他们的入门文档的cometd自己的库的最简单的客户端根本不会发送任何请求。没有tcp流量。有人知道我错过了什么吗?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

你必须有其他事情发生。

您的代码是正确的,CometD测试套件完全使用该代码进行数百次测试,并且工作正常。

请阅读troubleshooting sectionCometD documentation

作为建议,为org.cometd类别启用DEBUG日志记录级别,并查看您获得的日志:应该明白问题是什么。

如果您仍有问题,请随时将日志附加到CometD Issue Tracker的问题。