jetty 9 add websockets handler to handler list

不羁岁月 提交于 2019-12-12 04:08:51

问题


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


回答1:


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)


来源:https://stackoverflow.com/questions/34007087/jetty-9-add-websockets-handler-to-handler-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!