Java 如何使用tomcat启动web套接字应用程序?

Java 如何使用tomcat启动web套接字应用程序?,java,tomcat,websocket,Java,Tomcat,Websocket,我用JavaWebSocket进行了简单的聊天,但这不起作用,我不明白为什么 用于简单写入日志的服务器类: @ApplicationScoped @ServerEndpoint(value = "/index")// May be this mapping but it's don't work. public class ChatServer { private static final Logger LOGGER = Logger.getLogger(Chat

我用JavaWebSocket进行了简单的聊天,但这不起作用,我不明白为什么

用于简单写入日志的服务器类:

@ApplicationScoped
@ServerEndpoint(value = "/index")// May be this mapping but it's don't work.
public class ChatServer {
    private static final Logger LOGGER =
            Logger.getLogger(ChatServer.class.getName());

    @OnOpen
    public void onOpen(Session session) {
        LOGGER.log(Level.INFO, "New connection with client: {0}",
                session.getId());
    }

    @OnMessage
    public String onMessage(String message, Session session) {
        LOGGER.log(Level.INFO, "New message from Client [{0}]: {1}",
                new Object[] {session.getId(), message});
        return "Server received [" + message + "]";
    }

    @OnClose
    public void onClose(Session session) {
        LOGGER.log(Level.INFO, "Close connection for client: {0}",
                session.getId());
    }

    @OnError
    public void onError(Throwable exception, Session session) {
        LOGGER.log(Level.INFO, "Error for client: {0}", session.getId());
    }
}
我有一个简单的客户:

<html>
<head>
    <title>JEE7 WebSocket Example</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script>
        var chatClient = new WebSocket("ws://localhost:8080/index");

        chatClient.onmessage = function(evt) {
            var p = document.createElement("p");
            p.setAttribute("class", "server");
            p.innerHTML = "Server: " + evt.data;
            var container = document.getElementById("container");
            container.appendChild(p);
        };
        function send() {
            var input = document.getElementById("message");
            var p = document.createElement("p");
            p.setAttribute("class", "client");
            p.innerHTML = "Me: " + input.value;
            var container = document.getElementById("container");
            container.appendChild(p);
            chatClient.send(input.value);
            input.value = "";
        }
    </script>
</head>
<body>
<h1>JEE7 WebSocket Example</h1>
<div id="container">

</div>
<input type="text" id="message" name="message" />
<button type="button" id="send" onclick="send()">Send</button>
</body>
</html>

JEE7 WebSocket示例
var chatClient=newwebsocket(“ws://localhost:8080/index”);
chatClient.onmessage=函数(evt){
var p=document.createElement(“p”);
p、 setAttribute(“类”、“服务器”);
p、 innerHTML=“服务器:”+evt.data;
var container=document.getElementById(“容器”);
子容器(p);
};
函数send(){
var输入=document.getElementById(“消息”);
var p=document.createElement(“p”);
p、 setAttribute(“类”、“客户端”);
p、 innerHTML=“Me:”+input.value;
var container=document.getElementById(“容器”);
子容器(p);
chatClient.send(输入.value);
input.value=“”;
}
JEE7 WebSocket示例
发送
但是当我启动tomcat时,我有代码
404
。我认为在我的项目中没有足够的部分(类或一些配置文件)

这是我的项目结构:


所有类
ChatServer
标记为从未使用过。我认为这是不正常的。成功运行的项目中缺少什么?帮我解决这个问题。多谢各位

您在websocket中没有提到appname

                                                    //here
var chatClient = new WebSocket("ws://localhost:8080/appname/index");

我想这是你的打字错误,你是说appname?这是tomcat运行/调试配置的名称,或maven项目中的模块名称或其他名称?表示项目名称,您刚才提到了页面名称,但在localhost:8080中有许多项目,因此定义了包含索引页面的项目。我添加了/web base/,因为该模块名在war中打包,但在js控制台中出错:chat.html:27 WebSocket连接到“ws://localhost:8080/web base/index”失败:WebSocket握手期间出错:意外回复代码:404我想你需要看这段视频来帮助你-