iOS re-routing requests w/ GCDWebServer (not redirecting)

坚强是说给别人听的谎言 提交于 2019-12-24 18:51:14

问题


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.



来源:https://stackoverflow.com/questions/33002143/ios-re-routing-requests-w-gcdwebserver-not-redirecting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!