Websocket example won't work

11,464

I had the same problem trying to use WebSocket API on Tomcat 7.0.47. The error message being displayed client side wasn't any help at all and my server side endpoint was never being created.

After much wasted time I found it was due to way I had set the dependency for javax.websocket-api. I'm using Maven which has the default scope as compile which causes problems as Tomcat has the websocket-api.jar in its lib folder. Setting the dependency to provided solved it.

    <dependency>
      <groupId>javax.websocket</groupId>
      <artifactId>javax.websocket-api</artifactId>
      <version>1.0</version>
      <scope>provided</scope>
    </dependency>

Hope this helps

It's also worth noting that if running behind Apache you will need mod_proxy_wstunnel and if using IIS you will need version 8.0 as anything prior does not support websockets.

Share:
11,464

Related videos on Youtube

Nyger
Author by

Nyger

I'm new in this town :)

Updated on September 15, 2022

Comments

  • Nyger
    Nyger over 1 year

    I try run this example:

    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.websocket.OnClose;
    import javax.websocket.OnError;
    import javax.websocket.OnMessage;
    import javax.websocket.OnOpen;
    import javax.websocket.Session;
    import javax.websocket.server.ServerEndpoint;
    
    @ServerEndpoint(value = "/chat")
    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 use Tomcat 7.0.47, I check link: ws://localhost/Tests/chat. Do I need register this websocket or add some things in web.xml? Any idea why is not working for me?

  • GPrathap
    GPrathap over 8 years
    Really thank you bro.. I spent more than 4 hours to find this thing
  • Pascal
    Pascal about 8 years
    I had the reverse problem here: I put the dependency in provided early one but the rest of the coordinates were wrong. Fun fact, <groupId>javax</groupId> <artifactId>javaee-api</artifactId> also compiles but doesn't work. 5 hours lost...
  • rdguam
    rdguam about 8 years
    Thank you for this! Add 4 hours to everyone else's lost time. Also to add, if you use Tomcat 8, it works without having to specify provided.
  • zookastos
    zookastos over 4 years
    How does it work with travis? My travis builds are failiing with scope provided.
  • samael
    samael over 4 years
    @NalinAgrawal This is pretty old now. If you're using Tomcat 8 or 9 (or even later versions of 7) it's not required. Also, this is standard maven stuff. Using travis-ci does not make a difference
  • zookastos
    zookastos over 4 years
    So if I upgrade to tomcat 8, I wont even have to include this in my pom.xml? And if I have to, then can you please help me out with this question I posted - stackoverflow.com/questions/58490788/…
  • d_void
    d_void over 4 years
    This is not working for me, any ideas why? I tried it with tomcat 7.0.96, It still gives me 404 during handshake
  • samael
    samael over 4 years
    @d_void that's a different problem and should be another question with further detail of what code you have and what you have tried to resolve your problem.