运行WebAppContext与WebSocketHandler

运行WebAppContext与WebSocketHandler

本文介绍了我可以在同一个实例中运行WebAppContext与WebSocketHandler吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以一起运行WebSocketHandler和WebAppContext?我使用的是最新版本 9.2.1.v20140609 。我尝试配置下面,但在Websocket调用 localhost:8080 / WebAppContext拦截调用。这是我的Launcher:

is it possible to run WebSocketHandler and WebAppContext together? I'm using latest version 9.2.1.v20140609. I tried configuration below, but on Websocket call to localhost:8080/ WebAppContext intercepts call. Here is my Launcher:

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

}



我可以从启动器运行多个jetty实例,处理程序到一些 5555 端口,但最好是我想保留一个Jetty实例,也许使用 / ws 管理WebSocket连接

I can run from launcher multiple jetty instances and just wire WebSocket handler to some 5555 port, but preferably i would like to keep one Jetty instance, and maybe use /ws handler to manage WebSocket connections

推荐答案

如下所示:

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看起来像你想要的,需要一点

ContextHandlerCollection seems like what you want, takes a bit of getting used to chaining the jetty handlers together.

如果你不需要WebAppContext的功能,那么你可以删除它,只需直接使用ContextHandlers。

If you don't need the features of WebAppContext then you can remove that too and just use ContextHandlers directly.

我没有自己跑,但看起来不错。

I haven't ran this myself but it looks about right.

这篇关于我可以在同一个实例中运行WebAppContext与WebSocketHandler吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 19:45