我正在尝试连接到Twitter流API端点.它看起来像URLSession支持流式传输URLSessionStreamTask,但我无法弄清楚如何使用API.我也找不到任何示例代码.
我尝试测试以下内容,但没有记录网络流量:
let session = URLSession(configuration: .default, delegate: self, delegateQueue: nil)
let stream = session.streamTask(withHostName: "https://stream.twitter.com/1.1/statuses/sample.json", port: 22)
stream.startSecureConnection()
stream.readData(ofMinLength: 0, maxLength: 100000, timeout: 60, completionHandler: { (data, bool, error) in
print("bool = \(bool)")
print("error = \(String(describing: error))")
})
stream.resume()
Run Code Online (Sandbox Code Playgroud)
我也实现了委托方法(包括URLSessionStreamDelegate),但它们没有被调用.
如果某个代码发布了如何为chunked来自流端点的响应打开持久连接的示例,那将非常有用.此外,我正在寻求不涉及第三方库的解决方案.类似于/sf/answers/663165121/但使用URLSession等效更新的响应将是理想的.
注意:上面的示例代码中省略了授权信息.