我可以运行WebAppContext与WebSocketHandler在同一实例



是否可以一起运行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连接

不如这样写:

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。

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

@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社区被警告过,我问过类似的问题,因为没有人回答。他们建议我用另一种方法,但我会两种方法都试一试,并很快在这里留下答复。

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

相关内容

  • 没有找到相关文章

最新更新