如何使用独立Jetty进行服务器推送 [英] How to do server push with standalone Jetty

查看:108
本文介绍了如何使用独立Jetty进行服务器推送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用独立Jetty测试静态网站上的服务器推送功能。

I am trying to test the server push feature on a static website with standalone Jetty.

我的网站包含一个index.html + 1 CSS +一堆的图像。目录结构是:

My website consists of an index.html Â+ 1 CSS + a bunch of images. The directory structure is:

/Album
   Â|
   Â|-----index.html
   Â|-----style.css
   Â|------/images
        Â|
        Â|-----image 1.png
        Â|---a set of 100 images
        Â|---image100.png

因为我只是想快点测试服务器推送功能我没有将此网站编码为Java Web项目,因此没有任何web.xml文件。

As I just wanted to quickly test out the server push feature I did not code this website as a Java Web project and, therefore, didn't have any web.xml file.

但是,Jetty文档要求将PushCacheFilter添加到web.xml。因此,我在项目中创建了/Album/WEB-INF/web.xml,并添加了PushCacheFilter作为指定的文档。

However, the Jetty documentation asks to add PushCacheFilter to web.xml. Therefore, I created a /Album/WEB-INF/web.xml in my project and added the PushCacheFilter as the documentation specified.Â

首先,我无法理解从文档中可以看出PushCacheFilter的工作原理。
其次,我想控制哪些文件被推送,哪些文件不被推送。从文档来看,似乎PushCacheFilter并没有给我那种控制。

First, I am unable to understand from the documentation how exactly PushCacheFilter works. Secondly, I want to control which files get pushed and which do not. From the documentation, it seems PushCacheFilter doesn't give me that kind of control.

我在互联网上查了几个例子,但大多数都是嵌入式Jetty。
有人请帮我弄清楚如何在我的静态网站上用独立的Jetty测试服务器推送功能吗?

I have checked a few examples on Internet but most of them are with embedded Jetty. Will someone please help me to figure out how to test server push feature in my static website with standalone Jetty?

另外,我想问一下HTTP是否/ 2 Jetty的GitHub存储库中的客户端示例可以直接使用吗?对不起,我自己没有测试过客户端。我只是在存储库中看到它并且很好奇。如果有人能指出我使用Jetty的HTTP和HTTP / 2客户端的示例用例,我将非常感激。

Also, I wanted to ask whether the HTTP/2 client example in Jetty's GitHub repository directly usable as such? Sorry, haven't tested the client myself. I just saw it in the repository and was curious. I'll be really thankful if someone can point me in the direction of example use casewith Jetty's HTTP and HTTP/2 client.

推荐答案

问题太多而且不清楚你想做什么:)

Too many questions and it's not really clear what you want to do :)

Jetty在服务器上公开特定于Jetty的API以执行推送(最终,这些API将会成为Servlet 4.0的一部分。

Jetty exposes a Jetty-specific API on the server to perform pushes (eventually, these APIs will be part of Servlet 4.0).

您可以使用 org.eclipse.jetty.server.Request.getPushBuilder(),请参阅 http:/ /download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/server/PushBuilder.html

PushBuilder API将允许您设置要推送的资源并推送它。

The PushBuilder APIs will then allow you to setup a resource to push, and to push it.

PushCacheFilter 实现相关资源的缓存。
当请求具有相关辅助资源的主资源时, PushCacheFilter 使用 PushBuilder API。

PushCacheFilter implements a cache of correlated resources. When a primary resource that has correlated secondary resources is being requested, PushCacheFilter pushes those correlated resources using the PushBuilder APIs.

如果 PushCacheFilter 不符合您的需求,您可以使用自己的逻辑编写自己的过滤器,使用 PushBuilder API执行推送。

If PushCacheFilter does not fit your needs, you can write your own filter with your own logic and perform pushes using the PushBuilder APIs.

在客户端,如果要使用Java API执行请求并且接受推送,你必须使用 HTTP2Client ,参见 http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/http2/client/HTTP2Client.html

On the client side, if you want to use Java APIs to perform requests and receive pushes, you have to use HTTP2Client, see http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/http2/client/HTTP2Client.html.

您可以找到如何执行请求和接收推送的示例这里

You can find examples of how to perform a request and receive pushes here.

如果你想要一个与你的相似的完整示例(index.html +一堆图像),你可以查看 HTTP / 2演示

If you want a full fledged example similar to yours (index.html + bunch of images), you can look at the HTTP/2 demo.

更新:如何使用 PushBuilder 的简单示例。

UPDATE: Simple example of how to use PushBuilder.

public class MyPushFilter implements Filter {
@Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest)request;
        String uri = httpRequest.getRequestURI();
        switch (uri) {
            case "/index.html":
                // Jetty specific APIs for now.
                PushBuilder pushBuilder = Request.getBaseRequest(request).getPushBuilder();
                pushBuilder.path("/styles.css").push();
                pushBuilder.path("/background.png").push();
                break;
            default:
                break;
        }
        chain.doFilter(req, resp);
    }
}

上面的例子非常简单。它不处理HTTP版本,条件标题等。请查看 PushCacheFilter 这里,以便更好地实现。

The example above is very very simple. It does not handle HTTP version, conditional headers, etc. Please have a look at the implementation of PushCacheFilter here for a better implementation.

这篇关于如何使用独立Jetty进行服务器推送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆