繁体   English   中英

Java websocket服务器给404

[英]Java websocket server giving 404

我正在尝试构建一个java websocket服务器。 我写了一个简单的服务器端点:

@ServerEndpoint(value = "/test")
public class EndPoint {
    static Queue<Session> queue = new ConcurrentLinkedQueue<Session>();

    public static void send(int a, int b) {
        try {
            for(Session session : queue) {
                session.getBasicRemote().sendText("a = " + a + ",b=" + b);
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    @OnOpen
    public void openConnection(Session session) {
        queue.add(session);
    }

    @OnClose
    public void closeConnection(Session session) {
        queue.remove(session);
    }

    @OnError
    public void error(Session session, Throwable t) {
        queue.remove(session);
    }
}

我的web.xml看起来像这样:

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

<web-app version="2.5" 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_2_5.xsd">


</web-app>

当我从Javascript点击ws:// localhost:8080 / websocket-1.0 / test时,我收到了404响应。 我正在使用tomcat 8来部署它。 我在这里错过了什么?

1)使用最新版本的Tomcat 7.0.x或最新版本的Tomcat 8.0.x.

2)确保你的war只包含ServerEndpoint类和一个空的web.xml。 Tomcat自动扫描并加载使用ServerEndpoint注释注释的类

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1"
  metadata-complete="true">

</web-app>

3)验证catalina.out是否具有以下日志消息,并且没有异常。

org.apache.catalina.startup.HostConfig.deployWAR部署Web应用程序存档/usr/local/tomcat/apache-tomcat-8.0.12/webapps/.war

根据这个链接https://tyrus.java.net/documentation/1.4/index/websocket-api.html和我的经验,端点应该匹配ws:url

例如

@ServerEndpoint(value = "/test")
ws://localhost:8080/test

要么

@ServerEndpoint(value = "/websocket-1.0//test")
ws://localhost:8080/websocket-1.0/test

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM