Reputation: 1
I'm trying to connect to a cometd server via websocket client in my application.
Below are dependencies I added in pom.xml
.
<!-- All the cometd 8.0.2 dependencies Start -->
<!-- Client Dependencies Start -->
<dependency>
<groupId>org.cometd.java</groupId>
<artifactId>cometd-java-client-common</artifactId>
<version>8.0.2</version>
</dependency>
<dependency>
<groupId>org.cometd.java</groupId>
<artifactId>cometd-java-client-websocket-jetty</artifactId>
<version>8.0.2</version>
<!--<scope>test</scope>-->
</dependency>
<dependency>
<groupId>org.cometd.java</groupId>
<artifactId>cometd-java-client-http-common</artifactId>
<version>8.0.2</version>
</dependency>
<dependency>
<groupId>org.cometd.java</groupId>
<artifactId>cometd-java-client-http-jetty</artifactId>
<version>8.0.2</version>
</dependency>
<!-- Client Dependencies End -->
<!-- Server Dependencies Start -->
<dependency>
<groupId>org.cometd.java</groupId>
<artifactId>cometd-java-server-common</artifactId>
<version>8.0.2</version>
</dependency>
<dependency>
<groupId>org.cometd.java</groupId>
<artifactId>cometd-java-server-http-jakarta</artifactId>
<version>8.0.2</version>
</dependency>
<dependency>
<groupId>org.cometd.java</groupId>
<artifactId>cometd-java-server-http-jetty</artifactId>
<version>8.0.2</version>
</dependency>
<dependency>
<groupId>org.cometd.java</groupId>
<artifactId>cometd-java-annotation-server</artifactId>
<version>8.0.2</version>
</dependency>
<dependency>
<groupId>org.cometd.java</groupId>
<artifactId>cometd-java-annotation-server-jakarta</artifactId>
<version>8.0.2</version>
</dependency>
<dependency>
<groupId>org.cometd.java</groupId>
<artifactId>cometd-java-server-websocket-jakarta</artifactId>
<version>8.0.2</version>
</dependency>
<!-- Server Dependencies End -->
<!-- Common Dependencies Start -->
<dependency>
<groupId>org.cometd.java</groupId>
<artifactId>cometd-java-common</artifactId>
<version>8.0.2</version>
</dependency>
<!-- Common Dependencies End -->
<!-- All the cometd 8.0.2 dependencies end -->
I am using Jetty version 12.0.8
The application has built successfully, but when I run the applcation, it fails with 400 error code and MESSAGE: Unknown Bayeux Transport. Am I making any mistake in the dependencies which is leading to the error? Request you to kindly suggest.
I tried debugging the application.
The control goes to find
method of AbstactHttpTransport
which is part of comet-java-server-common-8.0.2.jar
.
The Transports are coming as
The find
method of AbstractHttpTransport
is returning null, and because of that getting the error Unknown Bayeux Transport.
public static AbstractHttpTransport find(BayeuxServer bayeuxServer, CometDRequest request) {
Iterator var2 = bayeuxServer.getAllowedTransports().iterator();
while(var2.hasNext()) {
String transportName = (String)var2.next();
ServerTransport serverTransport = bayeuxServer.getTransport(transportName);
if (serverTransport instanceof AbstractHttpTransport transport)
{
if (transport.accept(request)) {
return transport;
}
}
}
return null;
}
bayeuxServer.getAllowedTransports()
is returning 3
bayeuxServer.getTransport("websocket")
returns WebSocketTransport@40ebb56[websocket]
bayeuxServer.getTransport("long-polling")
returns JSONHttpTransport@116efe65[long-polling]
bayeuxServer.getTransport("callback-polling")
returns JSONPHttpTransport@2d97344c[callback-polling]
For Websocket transport it's not getting into below if
block
if (serverTransport instanceof AbstractHttpTransport transport) {
if (transport.accept(request)) {
return transport;
}
}
because WebSocketTransport
is an instance of AbstractWebSocketTransport
but not AbstractHttpTransport
.
So there is no way it can enter the if
block, hence the accept
method will not be executed.
For Long-polling, it gets into the first if
block since JSONHttpTransport
is the instance of AbstractHttpTransport
but the accept
method is returning false because the request method in our case is GET and if the request method is not POST then the accept
method is returning false.
Same is the case with JSONPHttpTransport
. Can you suggest me the solution to this problem?
But how to make this find
method return the proper BayeuxTransport? Please suggest if any changes to be done in the pom.xml
Upvotes: 0
Views: 100
Reputation: 18637
You likely did not set up WebSocket correctly.
When connecting via WebSocket, the HTTP upgrade request should be intercepted by Jetty's WebSocketUpgradeFilter
or WebSocketUpgradeHandler
, which would process the request and upgrade the communication to WebSocket.
After the upgrade, the code you reported above, AbstractHttpTransport.find()
, is not called, because the communication is not any more HTTP, but WebSocket.
You don't specify whether you are using Jetty as a standalone server, or embedded.
Please refer to the Jetty documentation here for how to set up Jetty embedded with WebSocket support, and here for how to set up WebSocket in Jetty as standalone server.
If you have further troubleshooting questions, please open a "question" issue at the CometD project here, as StackOverflow is not really suited for multiple back and forth troubleshooting.
Upvotes: 0