I try to listen data from dweet.io service. I use listen method to get data in realtime. It uses chunked HTTP responses.
I've created simple network manager for that
import Foundation
class NetworkManager: NSObject, URLSessionDataDelegate {
private lazy var session = URLSession(configuration: .default, delegate: self, delegateQueue: .main)
private lazy var task = self.session
.dataTask(with: URL(string: "https://dweet.io/listen/for/dweets/from/opposite-carpenter")!)
func start() {
self.task.resume()
}
// THIS METHOD IS NEVER CALLED
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
print(data)
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
print(error!.localizedDescription)
}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
print(response)
completionHandler(.allow)
}
}
When I start it I never get data. However I'm getting response with 200 status code and headers. And after about a minute I get an error The network connection was lost.
Simultaneously I use curl
for the same thing. And it's getting data as expected.
curl -i "https://dweet.io/listen/for/dweets/from/subdued-nation"
Another thing I was confused by is that iOS app and curl show me different values in Transfer-Encoding
field in headers:
UrlSession
returns "Transfer-Encoding" : "Identity"
.
At the same time curl shows Transfer-Encoding: chunked
(which is expected).
I've tried to apply approach described in this answer but I got the same result.