【问题标题】:Cometd Client not handshaking with cometD ServerCometd 客户端不与 cometD 服务器握手
【发布时间】:2019-01-05 16:24:43
【问题描述】:

我是 CometD 的新手。我用 Java 编写了一个基本的 CometD 服务器和一个简单的 CometD 客户端。我收到邮递员对 /meta/handshake、/meta/connect、/meta/subscribe 频道的成功响应。但是当我开始使用我的 cometD java 客户端(我从 https://protect-us.mimecast.com/s/vLH6CNk58of1Ow1GsmVz4u?domain=github.com 重用)时,握手失败并显示以下消息。

失败 {supportedConnectionTypes=[long-polling], 通道=/元/握手,id=22,版本=1.0}

我在我的代码中使用 cometdVersion - '4.0.0',jettyVersion - '9.4.0.v20161208', springbootVersion - '1.5.14.RELEASE'。 我已经对 AnnotationCometDServlet 进行了动态 servlet 注册,并添加了 /notifications 作为映射。 我在bayeuxServer 配置类中创建了如下频道。

bayeuxServerImpl.createChannelIfAbsent("/updates", 
(ServerChannel.Initializer) channel -> channel.addAuthorizer(GrantAuthorizer.GRANT_ALL));

在客户端代码中,我使用 /notifications 作为默认 URL,通道作为 /updates

@Service("cometListener")
@Slf4j
public class BayeuxListener implements BayeuxServer.SessionListener  {

@Inject
private BayeuxServer bayeuxServer;
@Session
private ServerSession serverSession;


@Configure({"/updates**,/notifications**"})
protected void configureChannel(ConfigurableServerChannel channel) {
    channel.addAuthorizer(GrantAuthorizer.GRANT_ALL);
    channel.addAuthorizer(GrantAuthorizer.GRANT_PUBLISH);
    channel.setPersistent(true);
}

@Listener("/meta/*")
public void monitorMeta(ServerSession session, ServerMessage message) {
    log.info("monitoring meta"+message.toString()+"channel "+message.getChannel()+"session id "+session.getId());
}

@Listener("/meta/subscribe")
public void monitorSubscribe(ServerSession session, ServerMessage message) {      
    log.info("Monitored Subscribe from " + session + " for " + message.get(Message.SUBSCRIPTION_FIELD));
}

@Listener("/meta/unsubscribe")
public void monitorUnsubscribe(ServerSession session, ServerMessage message) {
    log.info("Monitored Unsubscribe from " + session + " for " + message.get(Message.SUBSCRIPTION_FIELD));
}

@Listener("/updates")
public void handlesrgUpdates(ServerSession client, ServerMessage message) {
    ServerSession cilentSession = bayeuxServer.getSession(client.getId());
    client.deliver(cilentSession,"/updates", "Received message back from client");
}
}

【问题讨论】:

    标签: java spring-boot cometd


    【解决方案1】:

    你有 CometD 版本、Jetty 版本和 Spring Boot 版本的奇怪组合。我建议您坚持使用 CometD POM 中声明的默认版本控制,即 CometD 4.0.2、Jetty 9.4.14 和 Spring Boot 2.0.6。

    你提到的握手失败不完整或者不是握手回复失败。这是因为握手回复具有successful 字段,而您提到的{supportedConnectionTypes=[long-polling], channel=/meta/handshake, id=22, version=1.0} 看起来像握手请求。因此很难说是什么问题,因为失败原因通常会在握手回复中报告。

    如果您在/notifications Servlet 映射下动态注册了CometD Servlet,那么客户端应该有一个以/notifications 结尾的URL。 请注意,Servlet 映射 /notifications 和 CometD 通道 /notifications 是两个不同的东西并且不相关 - 它们只是碰巧有相同的名称。

    您的代码大部分都很好,但包含一些错误。

    @Configure({"/updates**,/notifications**"})
    protected void configureChannel(ConfigurableServerChannel channel) {
        channel.addAuthorizer(GrantAuthorizer.GRANT_ALL);
        channel.addAuthorizer(GrantAuthorizer.GRANT_PUBLISH);
        channel.setPersistent(true);
    }
    

    上面的代码必须改为:

    @Configure({"/updates/**,/notifications/**"})
    protected void configureChannel(ConfigurableServerChannel channel) {
        channel.addAuthorizer(GrantAuthorizer.GRANT_ALL);
        channel.setPersistent(true);
    }
    

    请注意,频道通配符必须在 / 之后。 在GRANT_ALL 之后不需要GRANT_PUBLISH,其中包括GRANT_PUBLISH。 配置方法应该是public,而不是protected。

    @Listener("/updates")
    public void handlesrgUpdates(ServerSession client, ServerMessage message) {
        ServerSession cilentSession = bayeuxServer.getSession(client.getId());
        client.deliver(cilentSession,"/updates", "Received message back from client");
    }
    

    无需从bayeuxServer 检索clientSession,因为它已经作为参数client 传递给方法。 该方法可以更好地实现为:

    @Listener("/updates")
    public void handlesrgUpdates(ServerSession client, ServerMessage message) {
        client.deliver(serverSession, "/updates", "Received message back from client");
    }
    

    注意消息的“发送者”是如何作为类的字段注入的serverSession 引用。

    上面的代码仍然可能是错误的。 因为/updates是一个广播频道,如果客户端订阅了/updates频道,当客户端向/updates频道发布消息时,它会从服务器接收回来(因为客户端订阅了@ 987654348@ 频道)并且上面的代码还将通过deliver() 在/updates 频道上向客户端发送另一个消息,因此客户端将在/updates 频道上收到两条不同的消息。 这可能是您想要的,但大多数时候并非如此。 请阅读difference between broadcast channels and service channels。

    使用握手失败的详细信息更新问题,并为 CometD、Jetty 和 Spring Boot 使用一致的版本控制。

    【讨论】:

    • 谢谢。更新 CometD、Jetty 和 Spring Boot 后,我​​可以在会话监听器中看到服务器中的客户端握手。我向 BayeuxServer Impl 添加了一个安全策略并覆盖了 canHandshake 方法。但我无法看到调试器点击 canHandshake()。你能帮我解决这个问题吗?
    • 我想在认证成功时添加一个 ServerSession 和唯一用户 ID 的映射。
    • 疑难解答讨论并不真正适用于 StackOverflow。欢迎您加入CometD mailing list 或file an issue。您可能在启动期间创建了多个 BayeuxServerImpl 实例,但在您的问题中您没有显示该部分。
    • 我还建议您从 CometD Spring Boot 示例代码开始,它可以正常工作。您从那里开始,然后根据需要对其进行修改。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多