我在我的应用程序中嵌入jetty以通过websockets进行通信,如何定义不同的路径?

时间:2015-02-26 06:18:59

标签: java websocket jetty embedded-jetty

我有一台需要与Web客户端实时通信的java服务器。为了做到这一点,我将嵌入Jetty 9. Jetty将作为Web客户端的静态Web服务器,以及websocket服务器。根据我在网上找到的例子,静态页面处理程序和WS处理程序都在同一条路径上提供服务。我不清楚如何让码头从两条不同的路径为它们服务。我会感激任何指针。这是我的代码

public static void main(String ...args) throws Exception {
    Server server = new Server(8080);

    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setDirectoriesListed(true);
    resource_handler.setWelcomeFiles(new String[]{ "index.html" });
    resource_handler.setResourceBase("./webapp");


    WebSocketHandler wsHandler = new WebSocketHandler() {
        @Override
        public void configure(WebSocketServletFactory factory) {
            factory.register(MyWebSocketHandler.class);
        }
    };

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { resource_handler,wsHandler, new DefaultHandler() });
    server.setHandler(handlers);

    server.start();
    server.join();
}

1 个答案:

答案 0 :(得分:2)

使用ContextHandlerCollection并将WebSockets包裹在ContextHandler个对象中。

    ContextHandlerCollection contexts = new ContextHandlerCollection();
    contexts.addHandler(new ContextHandler(wsFooHandler, "/foo"));
    contexts.addHandler(new ContextHandler(wsBarHandler, "/bar"));

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { resource_handler, contexts, new DefaultHandler() });

但是,使用ServletContextHandlerWebSocketUpgradeFilter及其addMapping可以让您以更强大的方式创建websocket控件。

示例:

public static void main(String... args) throws Exception
{
    Server server = new Server(8080);

    ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/");
    context.setWelcomeFiles(new String[] { "index.html" });

    WebSocketUpgradeFilter filter = WebSocketUpgradeFilter.configureContext(context);

    filter.addMapping(new ServletPathSpec("/foo/*"),new SingleWebSocketCreator(FooSocket.class));
    filter.addMapping(new ServletPathSpec("/bar/*"),new SingleWebSocketCreator(BarSocket.class));
    filter.addMapping(new ServletPathSpec("/*.ws"),new SingleWebSocketCreator(WsTypeSocket.class));
    filter.addMapping(new RegexPathSpec("/chat/room/[a-zA-z_]*/public"),new SingleWebSocketCreator(ChatSocket.class));

    // add your own non-websocket servlets
    context.addServlet(HelloServlet.class,"/hello");

    // Lastly, the default servlet for root content (serves up static content)
    // It is important that this is last.
    ServletHolder holderPwd = new ServletHolder("default", DefaultServlet.class);
    holderPwd.setInitParameter("resourceBase","./webapp");
    holderPwd.setInitParameter("dirAllowed","true");
    context.addServlet(holderPwd,"/");

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { context, new DefaultHandler() });
    server.setHandler(handlers);

    server.start();
    server.join();
}

而意外遗漏了SingleWebSocketCreator ......

public class SingleWebSocketCreator implements WebSocketCreator
{
    private Class<?> endpoint;

    public SingleWebSocketCreator(Class<?> websocketEndpoint)
    {
        this.endpoint = websocketEndpoint;
    }

    @Override
    public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp)
    {
        try
        {
            // new instance of endpoint for each upgrade
            return endpoint.newInstance();
        }
        catch (InstantiationException | IllegalAccessException e)
        {
            // failure to instantiate can flow out
            throw new RuntimeException(e);
        }
    }
}