"Unknown Bayeux Transport" 用于 websocket 传输 运行 cometd on Tomcat

"Unknown Bayeux Transport" for websocket transport running cometd on Tomcat

我正在尝试在我的 web 应用程序中将 CometD 与 websocket 传输一起使用,但是使用这种传输的握手失败并出现错误 "Unknown Bayeux Transport",并且 CometD 总是回退到长轮询。

我正在使用

这里是web.xml

<servlet>
  <servlet-name>cometd</servlet-name>
  <servlet-class>org.cometd.server.CometDServlet</servlet-class>
  <init-param>
    <param-name>jsonContext</param-name>
    <param-value>com.myapp.MyAppJettyJSONContextServer</param-value>
  </init-param>
  <init-param>
    <param-name>allowedTransports</param-name>
    <param-value>websocket,long-polling</param-value>
  </init-param>
  <init-param>
    <param-name>transports</param-name>
    <param-value>org.cometd.websocket.server.JettyWebSocketTransport,org.cometd.server.transport.AsyncJSONTransport</param-value>
  </init-param>
  <init-param>
    <param-name>ws.cometdURLMapping</param-name>
    <param-value>/cometd/*</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  <async-supported>true</async-supported>
</servlet>
<servlet-mapping>
  <servlet-name>cometd</servlet-name>
  <url-pattern>/cometd/*</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>configuration</servlet-name>
    <servlet-class>com.myapp.MyAppConfigurationServlet</servlet-class>
    <init-param>
      <param-name>SERVER_URL</param-name>
      <param-value>http://localhost:8380/myapp/cometd</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>

<filter>
  <filter-name>cross-origin</filter-name>
  <filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
  <async-supported>true</async-supported>
</filter>
<filter-mapping>
  <filter-name>cross-origin</filter-name>
  <url-pattern>/cometd/*</url-pattern>
</filter-mapping>

这里是MyAppConfigurationServlet.java

的代码
public class MyAppConfigurationServlet extends GenericServlet
{

    private static final long serialVersionUID = 6918716199131599352L;

    private static final Logger log = LoggerFactory.getLogger(MyAppConfigurationServlet.class);

    public static final String PUSH_SERVICE = "pushService";

    public static final String CONFIG_SERVER_URL = "SERVER_URL";

    @Override
    public void init() throws ServletException
    {
        WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        BayeuxServer bayeux = (BayeuxServer) getServletContext().getAttribute(BayeuxServer.ATTRIBUTE);
        String serverUrl = getInitParameter(CONFIG_SERVER_URL);
        log.debug("Creating PushService at url {}", serverUrl);
        PushService pushService = new PushService(bayeux, serverUrl);
        sharePushService(ctx, pushService);
    }

    private void sharePushService(WebApplicationContext ctx, PushService pushService)
    {
        try
        {
            PushServiceHolder container = (PushServiceHolder) ctx.getBean(PUSH_SERVICE);
            container.setPushService(pushService);
        }
        catch (BeansException e)
        {
            log.warn("Cannot find the bean {});
            log.trace("Error!", e);
        }
    }

    @Override
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
    {
        throw new ServletException();
    }

}

其中 PushServiceHolder 是一个简单的 spring bean,它使 PushService 对其他 bean 可用。

这是客户端实现

Map<String, Object> options = new HashMap<>();
JettyJSONContextClient jsonContext = new MyAppJettyJSONContextClient();
options.put(ClientTransport.JSON_CONTEXT_OPTION, jsonContext);

HttpClient httpClient = new HttpClient();
httpClient.setMaxConnectionsPerDestination(2);
httpClient.setConnectTimeout(60000);
httpClient.start();

WebSocketClient webSocketClient = new WebSocketClient(httpClient);
webSocketClient.start();
ClientTransport wsTransport = new JettyWebSocketTransport(options, null, webSocketClient);
BayeuxClient client = new BayeuxClient(url, wsTransport, longPollingTransport);
client.handshake();
client.waitFor(5000, BayeuxClient.State.CONNECTED);

websocket 传输的握手永远不会成功,带有 404 http 错误代码,因此 CometD 回退到长轮询。

调试到 org.cometd.server.CometDServlet 我找到了这部分

@Override
protected void service(HttpServletRequest request, HttpServletResponse     response) throws ServletException, IOException {
    if ("OPTIONS".equals(request.getMethod())) {
        serviceOptions(request, response);
        return;
    }

    AbstractHttpTransport transport = _bayeux.findHttpTransport(request);
    if (transport == null) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unknown Bayeux Transport");
    } else {
        transport.handle(request, response);
    }
}

org.cometd.websocket.server.JettyWebSocketTransport 不是 AbstractHttpTransport,因此此代码永远不会考虑它,即使它已正确配置为允许传输。
因此,websocket 传输的握手请求总是以错误 "Unknown Bayeux Transport".

结束

我使用 CometD 文档 here and here 创建了 web.xml
正如 CometD 团队多次建议的那样,我想看看 CometD maven primer,但是命令

$ mvn org.apache.maven.plugins:maven-archetype-plugin:2.4:generate -DarchetypeCatalog=http://cometd.org 

returns这个错误

[WARNING] Error reading archetype catalog http://cometd.org
org.apache.maven.wagon.TransferFailedException: URI does not specify a valid host name: http:/cometd.org
at org.apache.maven.wagon.providers.http.wagon.shared.AbstractHttpClientWagon.fillInputData (AbstractHttpClientWagon.java:1069)
at org.apache.maven.wagon.providers.http.wagon.shared.AbstractHttpClientWagon.fillInputData (AbstractHttpClientWagon.java:963)
at org.apache.maven.wagon.StreamWagon.getInputStream (StreamWagon.java:126)
at org.apache.maven.wagon.StreamWagon.getIfNewer (StreamWagon.java:88)
at org.apache.maven.wagon.StreamWagon.get (StreamWagon.java:61)
at org.apache.maven.archetype.source.RemoteCatalogArchetypeDataSource.downloadCatalog (RemoteCatalogArchetypeDataSource.java:119)
at org.apache.maven.archetype.source.RemoteCatalogArchetypeDataSource.getArchetypeCatalog (RemoteCatalogArchetypeDataSource.java:87)
at org.apache.maven.archetype.DefaultArchetypeManager.getRemoteCatalog (DefaultArchetypeManager.java:216)
at org.apache.maven.archetype.ui.generation.DefaultArchetypeSelector.getArchetypesByCatalog (DefaultArchetypeSelector.java:218)
at org.apache.maven.archetype.ui.generation.DefaultArchetypeSelector.selectArchetype (DefaultArchetypeSelector.java:71)
at org.apache.maven.archetype.mojos.CreateProjectFromArchetypeMojo.execute (CreateProjectFromArchetypeMojo.java:181)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:954)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: org.apache.maven.wagon.providers.http.httpclient.client.ClientProtocolException: URI does not specify a valid host name: http:/cometd.org
at org.apache.maven.wagon.providers.http.httpclient.impl.client.CloseableHttpClient.determineTarget (CloseableHttpClient.java:95)
at org.apache.maven.wagon.providers.http.httpclient.impl.client.CloseableHttpClient.execute (CloseableHttpClient.java:83)
at org.apache.maven.wagon.providers.http.wagon.shared.AbstractHttpClientWagon.execute (AbstractHttpClientWagon.java:825)
at org.apache.maven.wagon.providers.http.wagon.shared.AbstractHttpClientWagon.fillInputData (AbstractHttpClientWagon.java:986)
at org.apache.maven.wagon.providers.http.wagon.shared.AbstractHttpClientWagon.fillInputData (AbstractHttpClientWagon.java:963)
at org.apache.maven.wagon.StreamWagon.getInputStream (StreamWagon.java:126)
at org.apache.maven.wagon.StreamWagon.getIfNewer (StreamWagon.java:88)
at org.apache.maven.wagon.StreamWagon.get (StreamWagon.java:61)
at org.apache.maven.archetype.source.RemoteCatalogArchetypeDataSource.downloadCatalog (RemoteCatalogArchetypeDataSource.java:119)
at org.apache.maven.archetype.source.RemoteCatalogArchetypeDataSource.getArchetypeCatalog (RemoteCatalogArchetypeDataSource.java:87)
at org.apache.maven.archetype.DefaultArchetypeManager.getRemoteCatalog (DefaultArchetypeManager.java:216)
at org.apache.maven.archetype.ui.generation.DefaultArchetypeSelector.getArchetypesByCatalog (DefaultArchetypeSelector.java:218)
at org.apache.maven.archetype.ui.generation.DefaultArchetypeSelector.selectArchetype (DefaultArchetypeSelector.java:71)
at org.apache.maven.archetype.mojos.CreateProjectFromArchetypeMojo.execute (CreateProjectFromArchetypeMojo.java:181)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:954)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)

我的配置中缺少什么? CometD原型目录的正确地址是什么?

非常感谢

一般来说,CometD 建议部署在 Jetty 中,并使用最新版本(此时为 4.0.4)。

您的 web.xml transports init-param 的值不正确。

JettyWebSocketTransport 是 CometD 传输,在其实现中使用 Jetty-specific API;因此,它仅在您的应用程序部署在 Jetty 中时有效。

我建议您删除 transports init-param 并让 CometD 选择适当的默认值,这将在 Jetty 和 Tomcat 中工作。

郑重声明,默认的 WebSocket 传输是 org.cometd.websocket.server.WebSocketTransport,它基于由 Jetty 和 Tomcat 实现的标准 JSR 356 API。

您的客户端是正确的,尽管您可能还想使用标准的 WebSocket 传输,org.cometd.websocket.client.WebSocketTransport

我尝试了从原型生成项目的命令,对我来说效果很好。 也许您在错误中有提示:URI does not specify a valid host name: http:/cometd.org - 看看为什么只有 1 个斜杠,而不是所需的 2 个斜杠。

总结:

  • 移动到 CometD 4。0.x
  • 如果可以的话搬到码头
  • 删除 transports 配置并使用默认值。
  • Re-run 正确的原型 URL: https://cometd.org.