iOS re-routing requests w/ GCDWebServer (not redir

2019-08-14 19:07发布

问题:

I want to create a server on iOS with GCDWebServer, which will accept request to localhost, and then, draw the data from another url (a video file) and stream the data to the response. I intend to use plain NSURLConnection, and in the didReceiveData of the NSURLConnection callback, I want to pass this data to the GCDWebServerResponse.

I am having a hard time to figure out how can I keep the connection from a request open, so that I can initiate another request w/ NSURLConnection, and start serving data to the response.

Is there any way I can do that? Do I need to create a new subclass of GCDWebServerStreamedResponse?

Thank you in advance.

回答1:

You don't need to subclass GCDWebServerStreamedResponse but simply instantiate it and use the GCDWebServerAsyncStreamBlock callback.

In the callback, create your NSURLConnection and have it run asynchronously. Then whenever new data is available (e.g. from -didReceiveData:), pass it through using the GCDWebServerBodyReaderCompletionBlock and when there is no more data available, pass an empty NSData.

See the "Advanced asynchronous version" in the GCDWebServer README for a similar concept.