iOS 10中的HTTP / 2服务器推送

我正在努力让服务器推送在iOS 10上工作。我正在使用Akamai HTTP / 2演示。

https://http2.akamai.com/demo

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

@interface ViewController ()  @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 

不幸的是,日志显示fetch类型总是NSURLSessionTaskMetricsResourceFetchTypeNetworkLoad,当我希望它有时是NSURLSessionTaskMetricsResourceFetchTypeServerPush。 服务器显然支持它,如Web演示中所示。

 ... 2016-11-22 19:27:37.596205 HttpServerPush[2356:735927] protocol[h2] reuse[1] fetch:Network Load - http://sofzh.miximages.com/ios/tile-4.png 2016-11-22 19:27:37.596960 HttpServerPush[2356:735927] protocol[h2] reuse[1] fetch:Network Load - http://sofzh.miximages.com/ios/tile-5.png 2016-11-22 19:27:37.597877 HttpServerPush[2356:735927] protocol[h2] reuse[1] fetch:Network Load - http://sofzh.miximages.com/ios/tile-6.png 2016-11-22 19:27:37.603988 HttpServerPush[2356:735927] protocol[h2] reuse[1] fetch:Network Load - http://sofzh.miximages.com/ios/tile-1.png 2016-11-22 19:27:37.976911 HttpServerPush[2356:735927] protocol[h2] reuse[1] fetch:Network Load - http://sofzh.miximages.com/ios/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 - http://sofzh.miximages.com/ios/tile-8.png 2016-11-22 19:55:15.766 HttpServerPush[59822:1612935] protocol[http/1.1] reuse[1] fetch:Network Load - http://sofzh.miximages.com/ios/tile-11.png 2016-11-22 19:55:15.769 HttpServerPush[59822:1612935] protocol[http/1.1] reuse[1] fetch:Network Load - http://sofzh.miximages.com/ios/tile-9.png 2016-11-22 19:55:15.771 HttpServerPush[59822:1612935] protocol[http/1.1] reuse[1] fetch:Network Load - http://sofzh.miximages.com/ios/tile-12.png ... 

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

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

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

 $ nghttp -nv 'https://h2o.examp1e.net' | grep 'PUSH_PROMISE' [ 0.587] recv PUSH_PROMISE frame  [ 0.587] recv PUSH_PROMISE frame  [ 0.588] recv PUSH_PROMISE frame  [ 0.588] recv PUSH_PROMISE frame  [ 0.588] recv PUSH_PROMISE frame  $ 

我正要回答你的问题,iOS不支持HTTP / 2推送,但后来我快速浏览了一下页面:

https://www.shimmercat.com

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