iOS HTTP3

2021-11-18  本文已影响0人  tom__zhu

HTTP3是iOS15中一个全新特性#,这个特性也需要你们后端服务支持,接下来讲一下如何在你的APP中使用这个能力

客户端

服务端

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?

上一篇 下一篇

猜你喜欢

热点阅读