4

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

}

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

4

2 回答 2

4

怎么样:

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。

我自己没有运行过这个,但它看起来是正确的。

于 2014-07-08T10:40:25.567 回答
0

@Andrew,我接受你的回答。代码有效,但是当您连接到/ws它时会抛出302将您重定向到的/ws/,因此请连接到/ws/.

var ws = new WebSocket("ws://localhost:8080/ws");
WebSocket connection to 'ws://localhost:8080/ws' failed: Error during WebSocket handshake: Unexpected response code: 302

var ws = new WebSocket("ws://localhost:8080/ws/");

我在jetty-users社区中被警告过,我在那里问过类似的问题,因为没有人回答。他们建议我用另一种方式做,但我会尝试这两种方法,很快就会在这里回复。

编辑:决定坚持WebSocketServlet,并将其映射到现有的 WebAppContext

于 2014-07-08T19:50:51.410 回答