HTTP3是iOS15中一个全新特性#,这个特性也需要你们后端服务支持,接下来讲一下如何在你的APP中使用这个能力
客户端
- 需要真机才能支持HTTP3。
- 开发者模式中的HTTP/3选项对结果有一些影响,但实测发现Xcode联调才有影响。
- 如果请求的protocol一直是
h2
,那么断开Xcode联调,改用console查看输出的protocol,应该会不一样。 - HTTP协议可能会是h3,但有些情况下会返回h2或是h1.1,这是竞速情况下的正常现象,通常情况会是先h2然后再显示h3。这种特性是由<alt-svc>实现#。
- 另外charles不支持http3,所以连接代理无法发出http3请求,至于charles后续版本是否支持可以关注(Charles Version History)
服务端
NOTE: Servers that support HTTP3 should be based on Draft 29 or later.
使用姿势
- Install iOS 15.0 beta on a test device (HTTP/3 is not supported in the simulator).
- In Settings > Developer, under the Networking group, enable HTTP/3.
- Using Xcode 13.0 beta, create a new project from the iOS > App template. Set the Interface popup to Storyboard and the Language popup to Swift.
- try using the
assumesHTTP3Capable
property on theURLRequest
being used to execute the dataTask inURLSession
import UIKit
class ViewController: UIViewController, URLSessionDataDelegate {
private var session: URLSession!
@IBAction
private func testAction(_ sender: Any) {
if self.session == nil {
let config = URLSessionConfiguration.default
config.requestCachePolicy = .reloadIgnoringLocalCacheData
self.session = URLSession(configuration: config, delegate: self, delegateQueue: .main)
}
let urlStr = "<# Your H3 URL Here #>"
let url = URL(string: urlStr)!
var request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 60.0)
request.assumesHTTP3Capable = true
print("task will start, url: \\(url.absoluteString)")
self.session.dataTask(with: request) { (data, response, error) in
if let error = error as NSError? {
print("task transport error \\(error.domain) / \\(error.code)")
return
}
let response = response as! HTTPURLResponse
let data = data!
print("task finished with status \\(response.statusCode), bytes \\(data.count)")
}.resume()
}
func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
let protocols = metrics.transactionMetrics.map { $0.networkProtocolName ?? "-" }
print("protocols:", protocols)
}
}
HTTP/3 test servers
URL | Alt-Svc | Implemenation |
---|---|---|
quic.aiortc.org pgjones.dev | yes | aioquic |
cloudflare-quic.com quic.tech | yes | Cloudflare Quiche |
facebook.com fb.mvfst.net | no | mvfst |
quic.rocks | yes | Google quiche |
f5quic.com | no | F5 |
www.litespeedtech.com | yes | lsquic |
nghttp2.org | no | ngtcp2 |
test.privateoctopus.com | no | picoquic |
h2o.examp1e.net | yes | h2o/quicly |
quic.westus.cloudapp.azure.com | yes | msquic |
docs.trafficserver.apache.org | yes | Apache Traffic Server |