0

All handlers in that example work apart from the websockets handler

       WebSocketHandler wsHandler = new WebSocketHandler() {
        @Override
        public void configure(WebSocketServletFactory factory) {
            factory.register(WebsocketsService.class);
        }
    };

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { resource_handler, servletContextHandler, wsHandler, new DefaultHandler() });
    server.setHandler(handlers);

it fails with

WebSocket connection to 'ws://localhost:8080/' failed: Error during WebSocket handshake: Unexpected response code: 200

how would be a websockets handler properly configured and added (maybe with a differen Path and Port as the servletContextHandler or could it be added there ?) ?

user3732793
  • 1,699
  • 4
  • 24
  • 53

1 Answers1

2

A few things.

  1. Don't mix ResourceHandler and ServletContextHandler, use the built-in static file serving from ServletContextHandler, its Resource Base, and the DefaultServlet (see prior answer with details)
  2. Don't put anything after your ServletContextHandler (if your ServletContextHandler is on contextPath /). Once a ServletContextHandler is entered (per the contextPath), then it must complete/finish (this is part of the servlet spec), no other handler after that ServletContextHandler will ever run. (see prior answer about this)
  3. Don't mix WebSocketHandler and ServletContextHandler, use the WebSocketUpgradeFilter in your ServletContextHandler and add/manage the websocket endpoints there. (see the embedded-jetty-cookbook and the WebSocketViaFilter example for how to use it)
Community
  • 1
  • 1
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • Thanks for that. It's actually a lot to check. will come back if successfully. – user3732793 Dec 01 '15 at 14:16
  • it is a bit confusing that the Jetty Documentation actually mentions the Ressource Handler approach in some cases for Embedded solutions as in here [link](http://www.eclipse.org/jetty/documentation/current/embedded-examples.html) @Joakim you seem to be the right person to ask, so if you don't mind to comment on that ? – user3732793 Dec 01 '15 at 15:47
  • Again, *don't mix* `ResourceHandler` and `ServletContextHandler`, none of the examples in the documentation do that. See [the prior answer for more details on why](http://stackoverflow.com/questions/32378028/embedded-jetty-handling-urls-to-serve-content/32383973#32383973). – Joakim Erdfelt Dec 01 '15 at 15:51
  • Once you start using `ServletContextHandler` (which is superior btw), you no longer use any `Handler` that can serve content (things like `GzipHandler` are ok, as they just manipulate content generated from the `ServletContextHandler`, but don't generate their own content) – Joakim Erdfelt Dec 01 '15 at 15:53
  • unfortunatetly I get the "Unable to find resource directory" as handled here [link](https://github.com/jetty-project/embedded-jetty-cookbook/blob/master/src/main/java/org/eclipse/jetty/cookbook/DefaultServletFileServer.java) ... the path looks ok...should I post a new question about that ? – user3732793 Dec 01 '15 at 16:36
  • The "Unable to find resource directory" would be more appropriate as either an issue on github, or a new question on stackoverflow. – Joakim Erdfelt Dec 01 '15 at 18:56