我可以在同一个实例中使用WebSocketHandler运行WebAppContext吗?

时间:2014-07-05 00:54:45

标签: java configuration websocket jetty jetty-9

可以一起运行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();

}

我可以从启动器多个jetty实例运行,只需将WebSocket处理程序连接到某个5555端口,但最好我想保留一个Jetty实例,并且可能使用/ws处理程序来管理WebSocket连接< / p>

2 个答案:

答案 0 :(得分: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。

我自己没有跑过,但它看起来是正确的。

答案 1 :(得分: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社区发出警告,我在那里问了类似的question,因为没有人回答。他们建议我以其他方式这样做,但我会尝试这两种方法,并且很快会在这里留下答复。

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