如何将 WebSocketServlet 添加到具有上下文路径的嵌入式 Jetty 服务器?

How can I add a WebSocketServlet to an embedded Jetty server with context path?

我正在尝试在我正在处理的嵌入式 Jetty 应用程序中测试 WebSocket 支持。我的目标是将数据从服务器流式传输到浏览器。我还没有解决所有问题,因为我刚刚开始设置 WebSocket servlet/handler.

我看到的问题是 Chrome 无法连接到 WebSocket 处理程序:

与 'ws://127.0.0.1:8081/stream' 的 WebSocket 连接失败:WebSocket 握手期间出错:意外的响应代码:404 (匿名)@ ws-test.js:1

HTTP 404 来自 Jetty,因为它没有向“/stream”注册任何内容。

我这辈子都不知道如何使用指定的 URL 设置 WebSocketServlet 或 WebSocketHandler。我已经阅读了我能找到的所有示例和教程,但其中很多不是嵌入式 Jetty,就是过时了。无论哪种情况,我都愿意犯错。

我将从一些代码开始。这是我的服务器上下文和处理程序的主要 Jetty 设置:

ResourceHandler resource_handler = new ResourceHandler();
resource_handler.setWelcomeFiles(new String[] { "index.htm" });
resource_handler.setResourceBase('./www');

ServletHandler servletHandler = new ServletHandler();

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

// Add the /test servlet mapping
servletHandler.addServletWithMapping(TestServlet.class, "/test/*");

// Add websocket handler
handlers.addHandler(StreamingHandler.getServlet("/stream"));            
server.start();

这是我的 StreamingHandler class,它扩展了 WebSocketHandler。请注意,我正在尝试设置 WebSocketHandler 的上下文路径。重点是让 WebSocket 处理与 http://127.0.0.1:8081/stream:

的通信
@WebServlet
public class StreamingHandler extends WebSocketHandler
{
    public static ContextHandler getServlet(String url) {
        ContextHandler ctxHandler = new ContextHandler();
        ctxHandler.setContextPath(url);
        ctxHandler.setHandler(new StreamingHandler());
        return ctxHandler;
    }

    protected StreamingHandler() {
        super();
    }

    @Override
    public void configure(WebSocketServletFactory factory)
    {
        factory.getPolicy().setIdleTimeout(10000);
        factory.register(StreamingSocket.class);
    }
}

这是我的基本 WebSocket class:

@WebSocket
public class StreamingSocket {

    @OnWebSocketClose
    public void onClose(int statusCode, String reason) {
        System.out.println("Close: statusCode=" + statusCode + ", reason=" + reason);
    }

    @OnWebSocketError
    public void onError(Throwable t) {
        System.out.println("Error: " + t.getMessage());
    }

    @OnWebSocketConnect
    public void onConnect(Session session) {
        System.out.println("Connect: " + session.getRemoteAddress().getAddress());
        try {
            session.getRemote().sendString("Hello!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @OnWebSocketMessage
    public void onMessage(String message) {
    System.out.println("Message: " + message);
}

}

终于便宜了一点Javascript。我不会包括基本的 HTML。它只是引用了这个 JS:

var ws = new WebSocket("ws://127.0.0.1:8081/stream");

ws.onopen = function() {
    alert("Opened!");
    ws.send("Hello Server");
};

ws.onmessage = function (evt) {
    alert("Message: " + evt.data);
};

ws.onclose = function() {
    alert("Closed!");
};

ws.onerror = function(err) {
    alert("Error: " + err);
};

如有任何提示,我们将不胜感激。谢谢。

在经典形式中,我已经解决了这个问题,这要归功于 link Jetty 食谱,Joakim Erdfelt(谢谢!)在这里回答了另一个 SO 问题:

基本上我在这里查看了示例 WebSocketServerViaFilter 示例: https://github.com/jetty-project/embedded-jetty-cookbook/blob/master/src/main/java/org/eclipse/jetty/cookbook/websocket/WebSocketServerViaFilter.java

我没见过这种用法,菜谱也够新了,还行。这是我的新主服务器代码(请注意,如果您希望为您的资源提供服务,则必须添加 DefaultServlet):

Path webRootPath = new File(www).toPath().toRealPath();

ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.setBaseResource(new PathResource(webRootPath));
context.setWelcomeFiles(new String[] { "index.html" });
server.setHandler(context);

// Add the websocket filter
WebSocketUpgradeFilter wsfilter = WebSocketUpgradeFilter.configureContext(context);
wsfilter.getFactory().getPolicy().setIdleTimeout(5000);
wsfilter.addMapping(new ServletPathSpec("/stream"), new StreamingSocketCreator());

// Add the /test servlet mapping
ServletHolder holderTest = new ServletHolder("test", TestServlet.class);
holderTest.setInitParameter("dirAllowed","true");
context.addServlet(holderTest,"/test/*");

// NOTE! If you don't add the DefaultServlet, your 
// resources won't get served!
ServletHolder holderDefault = new ServletHolder("default", DefaultServlet.class);
holderDefault.setInitParameter("dirAllowed", "true");
context.addServlet(holderDefault, "/");

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

然后我创建了一个 WebSocketCreator 实现。这似乎是一个不必要的步骤,但它是 API 的工作原理:

public class StreamingSocketCreator implements WebSocketCreator
{
    @Override
    public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp)
    {
        return new PacketStreamingSocket();
    }
}

相同的 WebSocket 代码适用:

@WebSocket
public class StreamingSocket {

    @OnWebSocketClose
    public void onClose(int statusCode, String reason) {
        System.out.println("Close: statusCode=" + statusCode + ", reason=" + reason);
    }

    @OnWebSocketError
    public void onError(Throwable t) {
        System.out.println("Error: " + t.getMessage());
    }

    @OnWebSocketConnect
    public void onConnect(Session session) {
        System.out.println("Connect: " + session.getRemoteAddress().getAddress());
        try {
            session.getRemote().sendString("Hello!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @OnWebSocketMessage
    public void onMessage(String message) {
    System.out.println("Message: " + message);
}

终于,我的JavaScript代码可以找到WebSocket上下文路径了!

var ws = new WebSocket("ws://127.0.0.1:8081/stream");

ws.onopen = function() {
    alert("Opened!");
    ws.send("Hello Server");
};

ws.onmessage = function (evt) {
    alert("Message: " + evt.data);
};

ws.onclose = function() {
    alert("Closed!");
};

ws.onerror = function(err) {
    alert("Error: " + err);
};

它们协同工作。现在我可以测试服务器 -> 客户端数据流。谢谢,我希望这对以后的其他人有所帮助。