本文介绍了码头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 ?) ?
推荐答案
一些事情.
- 请勿混合使用
ResourceHandler
和ServletContextHandler
,使用从ServletContextHandler
,其资源库和DefaultServlet
提供的内置静态文件(请参阅) - 请勿在
ServletContextHandler
之后放置任何内容(如果您的ServletContextHandler
位于contextPath/
上).一旦输入了ServletContextHandler
(通过contextPath),它就必须完成/完成(这是servlet规范的一部分),该ServletContextHandler
之后的其他处理程序将永远不会运行. (请参见有关此问题的先前答案) - 请勿混用
WebSocketHandler
和ServletContextHandler
,在ServletContextHandler
中使用WebSocketUpgradeFilter
,然后在其中添加/管理websocket端点. (请参见 embedded-jetty-cookbook 和 示例(如何使用)
- Don't mix
ResourceHandler
andServletContextHandler
, use the built-in static file serving fromServletContextHandler
, its Resource Base, and theDefaultServlet
(see prior answer with details) - Don't put anything after your
ServletContextHandler
(if yourServletContextHandler
is on contextPath/
). Once aServletContextHandler
is entered (per the contextPath), then it must complete/finish (this is part of the servlet spec), no other handler after thatServletContextHandler
will ever run. (see prior answer about this) - Don't mix
WebSocketHandler
andServletContextHandler
, use theWebSocketUpgradeFilter
in yourServletContextHandler
and add/manage the websocket endpoints there. (see the embedded-jetty-cookbook and theWebSocketViaFilter
example for how to use it)
这篇关于码头9将websockets处理程序添加到处理程序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!