简体   繁体   English

如何使用tomcat启动网络套接字应用程序?

[英]How to start web-socket app with tomcat?

I have simple chat with Java web-socket, but this is don't work and I don't understand why. 我可以通过Java网络套接字进行简单的聊天,但这是行不通的,我也不知道为什么。

Server class which simple write log: 服务器类里面简单的写日志:

@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());
    }
}

I have a simple client: 我有一个简单的客户:

<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>

But when I start tomcat I have code 404 . 但是当我启动tomcat时,我的代码是404 I think in my project not enough of something part(Classes or some config files). 我认为在我的项目中,某些部分(类或某些配置文件)不够。

This is structure of my project: 这是我的项目的结构: 在此处输入图片说明

All class ChatServer mark as never used. 所有类ChatServer标记为从未使用。 I think this is not normal. 我认为这是不正常的。 what is missing in my project for run successfully? 我的项目中缺少什么才能成功运行? Help me fix this issue. 帮助我解决此问题。 Thank You. 谢谢。

You did not mention appname in websocket. 您没有在websocket中提及应用名称。

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

I think This is your typo mistake 我想这是你的错字

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

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