Pavel
Pavel

Reputation: 2101

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.

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. I think in my project not enough of something part(Classes or some config files).

This is structure of my project: enter image description here

All class ChatServer mark as never used. I think this is not normal. what is missing in my project for run successfully? Help me fix this issue. Thank You.

Upvotes: 0

Views: 381

Answers (1)

Mamtora Mohit
Mamtora Mohit

Reputation: 34

You did not mention appname in websocket.

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

I think This is your typo mistake

Upvotes: 1

Related Questions