java-我可以在同一实例中使用WebSocketHandler运行WebAppContext吗?
作者:互联网
是否可以同时运行WebSocketHandler和WebAppContext?我正在使用最新版本9.2.1.v20140609.我在下面尝试了配置,但是在Websocket上调用localhost:8080 / WebAppContext截获了调用.这是我的启动器:
public static void main(String[] args) throws Exception
{
ServerConnector connector = new ServerConnector(server);
connector.setPort(8080);
server.addConnector(connector);
WebAppContext context = new WebAppContext("webapp", "/");
// Setting up browser caching. Binds params for org.eclipse.jetty.servlet.DefaultServlet.init()
context.getInitParams().put("org.eclipse.jetty.servlet.Default.etags", "true");
context.getInitParams().put("org.eclipse.jetty.servlet.Default.cacheControl", "public, max-age=0");
// Fix for Windows, so Jetty doesn't lock files
if (System.getProperty("os.name").toLowerCase().contains("windows")) {
context.getInitParams().put("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false");
}
// Will throw an exception when will be unable to start server for some reason
context.setThrowUnavailableOnStartupException(true);
Broker broker = new Broker();
// Implementation of org.eclipse.jetty.websocket.server.WebSocketHandler
WebSocketHandler socketHandler = new com.namespace.websocket.Handler(broker);
HandlerCollection handlerCollection = new HandlerCollection();
handlerCollection.setHandlers(new Handler[] {context, socketHandler});
server.setHandler(handlerCollection);
// Remove Server:Jetty(9...) from Response Headers
removeServerVersionFromHeaders(server);
server.start();
}
我可以从启动器运行多个码头实例,只需将WebSocket处理程序连接到某个5555端口,但最好是我想保留一个Jetty实例,并可能使用/ ws处理程序来管理WebSocket连接
解决方法:
怎么样:
public class Websock {
private static class Adapter extends WebSocketAdapter {
@Override
public void onWebSocketConnect(Session sess) {
System.out.print("client connected");
}
}
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
WebAppContext context = new WebAppContext("webapp", "/");
context.getServletHandler().setEnsureDefaultServlet(false); // may or may not be needed.
ContextHandlerCollection handlerCollection = new ContextHandlerCollection();
handlerCollection.addHandler(context);
handlerCollection.addHandler(createWebsocketHandler());
server.setHandler(handlerCollection);
server.start();
}
private static ContextHandler createWebsocketHandler() {
ContextHandler contextHandler = new ContextHandler("/ws");
contextHandler.setAllowNullPathInfo(true); // disable redirect from /ws to /ws/
final WebSocketCreator webSocketcreator = new WebSocketCreator() {
public Object createWebSocket(ServletUpgradeRequest request,
ServletUpgradeResponse response) {
return new Adapter();
}
};
Handler webSocketHandler = new WebSocketHandler() {
public void configure(WebSocketServletFactory factory) {
factory.setCreator(webSocketcreator);
}
};
contextHandler.setHandler(webSocketHandler);
return contextHandler;
}
}
ContextHandlerCollection看起来像您想要的,需要一些时间来将码头处理程序链接在一起.
如果您不需要WebAppContext的功能,则也可以将其删除,而直接使用ContextHandlers.
我自己还没有运行过,但是看起来不错.
标签:websocket,configuration,jetty,jetty-9,java 来源: https://codeday.me/bug/20191029/1959511.html