Establish websocket in cometd unsuccessful

1.6k views Asked by At

I write an application using cometd with bayeux server to send/receive message between server and client

But when cometd create websocket to server, server always return code 400:

"WebSocket connection to 'ws://localhost:8080/APServer/cometd' failed: Unexpected response code: 400"

My client configure cometd:

cometd.configure({
    url: cometURL,
    logLevel: 'debug',
    appendMessageTypeToURL: false
});
cometd.addListener('/meta/handshake', _metaHandshake);
cometd.addListener('/meta/connect', _metaConnect);
cometd.websocketEnabled = true;
cometd.handshake();

My server web.xml as below:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     version="3.0">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>3.main.html</welcome-file>
    </welcome-file-list>
   <servlet>
        <servlet-name>cometd</servlet-name>
        <servlet-class>org.cometd.server.CometdServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        <!--init-param>
            <param-name>transports</param-name>
            <param-value>org.cometd.websocket.server.WebSocketTransport</param-value>
        </init-param-->
        <async-supported>true</async-supported>
        <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>
    <servlet>
        <servlet-name>configuration</servlet-name>
        <servlet-class>jp.co.ntt.lab.multiline.web.init.ConfigurationServlet</servlet-class>
        <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>
</web-app>
1

There are 1 answers

0
sbordet On

From your web.xml you have commented out the section that specifies additional transports and that actually defines the WebSocket transport.

Therefore your server does not have WebSocket support (because you have commented it out), and that explains why your client gets the error that you report.

If you uncomment that section WebSocket should just work.