1

我有一个彗星的例子,我正在努力。该示例提供了一个 javascript 客户端代码。我正在尝试创建一个可以连接并执行相同操作的 Java 客户端。唯一的问题是我无法连接。

当我打电话时handshake();,它返回:

java.net.ProtocolException: Unexpected response 404: TransportExchange@f9e597=POST//localhost:8080/cometd/login/handshake#CONTENT(0ms)->COMPLETED(0ms)sent=45ms

我不确定到底发生了什么。这是Javascript客户端代码。当用户单击页面上的按钮时,它会被调用。它执行握手。

            $('#connect').click(function() {
            var u = $('#user').val();
            if(u) {
                $.post('login', {
                    user: u
                }, function() {
                    _disconnecting = false;
                    cometd.handshake();
                });
            }
        });

这是我用来尝试复制它的客户端代码。我http://localhost:8080/cometd/login进入它:

public void handshake(String url) throws Exception {
    // Create (and eventually setup) Jetty's HttpClient
    HttpClient httpClient = new HttpClient();
    // Here setup Jetty's HttpClient, for example:
    // httpClient.setMaxConnectionsPerAddress(2);
    httpClient.start();

    // Prepare the transport
    Map<String, Object> options = new HashMap<String, Object>();
    ClientTransport transport = LongPollingTransport.create(options, httpClient);

    ClientSession client = new BayeuxClient(url, transport);
    // Here setup the BayeuxClient, for example:
    client.getChannel(Channel.META_HANDSHAKE).addListener(new ClientSessionChannel.MessageListener() {
        public void onMessage(ClientSessionChannel channel, Message message) {
            if (message.isSuccessful()) {
                System.out.println("HandShake yea!!");
            }
        }
    });
    client.handshake();
}

在服务器上有一个正在运行的 servlet。我有断点。我可以看到使用 javascript 版本单击按钮时正在调用它。但是,当我使用我的版本时,它永远不会被调用。

如果有帮助,这里是 servlet:

public final class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println(getUrl(req));
    HttpSession session = req.getSession();
    String existingUser = (String) session.getAttribute("user");
    String user = req.getParameter("user");
    if (user != null && user.trim().length() == 0) {
        user = null;
    }
    if (user == null && existingUser == null) {
        resp.setStatus(HttpServletResponse.SC_FORBIDDEN);
    } else if (existingUser != null && !existingUser.equals(user)) {
        resp.setStatus(HttpServletResponse.SC_FORBIDDEN);
    } else if (user != null && existingUser == null) {
        session.setAttribute("user", user);
        resp.setStatus(HttpServletResponse.SC_OK);
    }
}
}

这是 web.xml 文件:

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

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:j2ee="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_3.0.xsd">

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>login</servlet-name>
    <servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/login</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>cometd</servlet-name>
    <servlet-class>org.cometd.java.annotation.AnnotationCometdServlet</servlet-class>
    <async-supported>true</async-supported>
    <init-param>
        <param-name>timeout</param-name>
        <param-value>60000</param-value>
    </init-param>
    <init-param>
        <param-name>logLevel</param-name>
        <param-value>3</param-value>
    </init-param>
    <init-param>
        <param-name>services</param-name>
        <param-value>ChatService</param-value>
    </init-param>
    <init-param>
        <param-name>transports</param-name>
        <param-value>com.ovea.cometd.websocket.jetty8.Jetty8WebSocketTransport</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>cometd</servlet-name>
    <url-pattern>/cometd/*</url-pattern>
</servlet-mapping>

4

0 回答 0