【问题标题】:HTTP/2 Server Push in iOS 10iOS 10 中的 HTTP/2 服务器推送
【发布时间】:2017-04-06 22:46:14
【问题描述】:

我正在尝试在 iOS 10 上运行服务器推送。我正在使用 Akamai HTTP/2 演示。

https://http2.akamai.com/demo

以下是我测试服务器推送的尝试。

@interface ViewController () <NSURLSessionTaskDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];

    NSString *displayArtUrl;
    for(int i=0; i<378; i++) {
        displayArtUrl = [NSString stringWithFormat:@"https://http2.akamai.com/demo/tile-%ld.png", (long)i];
        NSURL *url = [NSURL URLWithString:displayArtUrl];
        NSURLSessionDataTask *downloadTask = [defaultSession
                                              dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                              }];
        [downloadTask resume];
    }

}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics {
    NSArray *fetchTypes = @[ @"Unknown", @"Network Load", @"Server Push", @"Local Cache" ];

    for(NSURLSessionTaskTransactionMetrics *transactionMetrics in [metrics transactionMetrics]) {

        NSLog(@"protocol[%@] reuse[%d] fetch:%@ - %@", [transactionMetrics networkProtocolName], [transactionMetrics isReusedConnection], fetchTypes[[transactionMetrics resourceFetchType]], [[transactionMetrics request] URL]);

        if([transactionMetrics resourceFetchType] == NSURLSessionTaskMetricsResourceFetchTypeServerPush) {
            NSLog(@"Asset was server pushed");
        }
    }
}

@end

不幸的是,日志显示获取类型始终是 NSURLSessionTaskMetricsResourceFetchTypeNetworkLoad,而我有时希望它是 NSURLSessionTaskMetricsResourceFetchTypeServerPush。如网络演示所示,服务器显然支持它。

...
2016-11-22 19:27:37.596205 HttpServerPush[2356:735927] protocol[h2] reuse[1] fetch:Network Load - https://http2.akamai.com/demo/tile-4.png
2016-11-22 19:27:37.596960 HttpServerPush[2356:735927] protocol[h2] reuse[1] fetch:Network Load - https://http2.akamai.com/demo/tile-5.png
2016-11-22 19:27:37.597877 HttpServerPush[2356:735927] protocol[h2] reuse[1] fetch:Network Load - https://http2.akamai.com/demo/tile-6.png
2016-11-22 19:27:37.603988 HttpServerPush[2356:735927] protocol[h2] reuse[1] fetch:Network Load - https://http2.akamai.com/demo/tile-1.png
2016-11-22 19:27:37.976911 HttpServerPush[2356:735927] protocol[h2] reuse[1] fetch:Network Load - https://http2.akamai.com/demo/tile-7.png
....

有没有人在 iOS 10 上成功使用 HTTP/2 服务器推送?请求资产的方式是否缺少某些内容?

仅供参考,Charles Proxy 似乎在这种情况下妨碍了工作。启用它会导致 iOS 10 完全停止使用 HTTP/2。

...
2016-11-22 19:55:15.763 HttpServerPush[59822:1612935] protocol[http/1.1] reuse[1] fetch:Network Load - https://http2.akamai.com/demo/tile-8.png
2016-11-22 19:55:15.766 HttpServerPush[59822:1612935] protocol[http/1.1] reuse[1] fetch:Network Load - https://http2.akamai.com/demo/tile-11.png
2016-11-22 19:55:15.769 HttpServerPush[59822:1612935] protocol[http/1.1] reuse[1] fetch:Network Load - https://http2.akamai.com/demo/tile-9.png
2016-11-22 19:55:15.771 HttpServerPush[59822:1612935] protocol[http/1.1] reuse[1] fetch:Network Load - https://http2.akamai.com/demo/tile-12.png
...

【问题讨论】:

    标签: ios objective-c ios10 http2


    【解决方案1】:

    据我所知,Akamai 演示页面目前没有推送任何资源。当服务器将PUSH_PROMISE 帧发送到客户端时,就会发生推送。 nghttp(可通过 brew 在 Mac OS X 上安装:brew install nghttp2)可用于显示服务器发送的帧:

    $ nghttp -nv 'https://http2.akamai.com/demo' | grep 'PUSH_PROMISE'
    $
    

    另一方面,H2O HTTP服务器的主页https://h2o.examp1e.net/确实推送资源:

    $ nghttp -nv 'https://h2o.examp1e.net' | grep 'PUSH_PROMISE'
    [  0.587] recv PUSH_PROMISE frame <length=59, flags=0x04, stream_id=13>
    [  0.587] recv PUSH_PROMISE frame <length=33, flags=0x04, stream_id=13>
    [  0.588] recv PUSH_PROMISE frame <length=35, flags=0x04, stream_id=13>
    [  0.588] recv PUSH_PROMISE frame <length=24, flags=0x04, stream_id=13>
    [  0.588] recv PUSH_PROMISE frame <length=28, flags=0x04, stream_id=13>
    $
    

    【讨论】:

    • 感谢您将我指向 nghttp。这是非常有帮助的。看起来我的错误是假设 Akamai HTTP/2 演示是高性能的,因为它使用的是 HTTP/2 服务器推送。由于多路复用,它看起来很高效。
    【解决方案2】:

    HTTP/2 Push 被广泛认为在 Safari 中被破坏。 不要认为这些信息是最新的,但上次我检查浏览器会接受并且有时会使用推送的资源,而其他时候则不会。

    原始答案

    我正准备回答你,iOS 不支持 HTTP/2 Push,但后来我快速浏览了我们的页面:

    https://www.shimmercat.com

    并等待连接统计信息出现在右上角。 iOS 10 支持 HTTP/2 推送!!

    【讨论】:

    • 不确定“出现在右上角”的“连接统计信息”是什么意思。很想验证 HTTP/2 服务器推送在 iOS 上是否有效,但这似乎表明它在 iOS 上不受支持:caniuse.com/#feat=push-api
    • @NicholasWestby Push APIPush NotificationServer Push 不同
    • 谢谢@alireza,我确实链接到了错误的功能。可悲的是,似乎服务器推送最终会消失,所以可能不值得进一步投资:evertpot.com/http-2-push-is-dead
    • @NicholasWestby 哇!这令人震惊,对我来说是出乎意料的。很高兴你告诉我这件事。
    猜你喜欢
    • 2017-09-06
    • 2019-05-24
    • 1970-01-01
    • 2018-07-26
    • 2017-03-04
    • 2019-10-10
    • 2020-04-23
    • 2011-08-17
    • 2020-03-24
    相关资源
    最近更新 更多