iOS HTTP3

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.

使用姿势

  1. Install iOS 15.0 beta on a test device (HTTP/3 is not supported in the simulator).
  2. In Settings > Developer, under the Networking group, enable HTTP/3.
  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.
  4. try using the assumesHTTP3Capable property on the URLRequest being used to execute the dataTask in URLSession
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

参考

HTTP/3 in your App
Who is ready for HTTP/3?

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容