0

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.

  • Let's make sure the problem is with the URL and not with your code. If you change `URL(string: "https://dweet.io/listen/for/dweets/from/opposite-carpenter")` to `URL(string: "https://www.apple.com")`, does the data get printed in the console? – matt Mar 04 '19 at 21:34
  • Also you say "after about a minute". That sounds like you're timing out, since one minute is exactly the default value of a URLSessionConfiguration `timeoutIntervalForRequest`. Is this a connection that needs to be kept open for longer than that? – matt Mar 04 '19 at 21:37
  • @matt you're right. If I use apple.com as url that's working fine. Cause that's ordinary HTTP request. But how can I handle HTTPS-streams? – Nick Kibish Mar 04 '19 at 21:45

1 Answers1

0

I'm going to guess that the problem is the phrase URLSession(configuration: .default. Using the default configuration, you time out after one minute. That, presumably, is not what you want.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Definitely no. Within this minute I can create few data chunks, get HTTP response in NetworkManager, get data with `curl` but don't get data in session delegate. – Nick Kibish Mar 04 '19 at 21:43
  • Well it was just a wild guess! Sorry not to be any help. :( – matt Mar 04 '19 at 22:20