本文介绍了码头9将websockets处理程序添加到处理程序列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该示例中的所有处理程序都与websockets处理程序分开工作

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

失败

与'ws://localhost:8080/'的WebSocket连接失败:WebSocket握手期间出错:意外的响应代码:200

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

如何正确配置和添加一个websockets处理程序(也许将不同的Path和Port作为servletContextHandler或可以在其中添加?)?

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. 请勿混合使用ResourceHandlerServletContextHandler,使用从ServletContextHandler,其资源库和DefaultServlet提供的内置静态文件(请参阅)
  2. 请勿在ServletContextHandler之后放置任何内容(如果您的ServletContextHandler位于contextPath /上).一旦输入了ServletContextHandler(通过contextPath),它就必须完成/完成(这是servlet规范的一部分),该ServletContextHandler之后的其他处理程序将永远不会运行. (请参见有关此问题的先前答案)
  3. 请勿混用WebSocketHandlerServletContextHandler,在ServletContextHandler中使用WebSocketUpgradeFilter,然后在其中添加/管理websocket端点. (请参见 embedded-jetty-cookbook 示例(如何使用)
  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)

这篇关于码头9将websockets处理程序添加到处理程序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-16 19:20