iOS 10中的HTTP/2服务器推送 [英] HTTP/2 Server Push in iOS 10

查看:241
本文介绍了iOS 10中的HTTP/2服务器推送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get server push working on iOS 10. I am working off of the Akamai HTTP/2 demo.

https://http2.akamai.com/demo

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

The following is my attempt at testing server push.

@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.如Web演示所示,服务器显然支持它.

Unfortunately, the logs show that the fetch type is always NSURLSessionTaskMetricsResourceFetchTypeNetworkLoad when I would expect it to be NSURLSessionTaskMetricsResourceFetchTypeServerPush sometimes. The server obviously supports it, as seen in the web demo.

...
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服务器推送吗?请求资产的方式中是否缺少某些东西?

Has anyone had success with HTTP/2 server push on iOS 10? Is there something missing in the way the assets are being requested?

仅供参考,在这种情况下,查尔斯·普林斯(Charles Proxy)似乎很碍事.启用它会导致iOS 10完全停止使用HTTP/2.

FYI, Charles Proxy seems to get in the way in this scenario. Enabling it causes iOS 10 to stop using HTTP/2 entirely.

...
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
...

推荐答案

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

As far as I know, the Akamai demo page doesn't currently push any resource. A push occurs when a PUSH_PROMISE frame is sent by the server to the client. nghttp (installable via brew on Mac OS X: brew install nghttp2) can be used to display the frames sent by the server:

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

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

On the other hand, https://h2o.examp1e.net/, the home page of the H2O HTTP server, does push resources:

$ 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>
$

这篇关于iOS 10中的HTTP/2服务器推送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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