带GCDWebServer的iOS重新路由请求(不重定向) [英] iOS re-routing requests w/ GCDWebServer (not redirecting)
问题描述
我想用GCDWebServer
在iOS上创建服务器,该服务器将接受对localhost
的请求,然后从另一个url
(视频文件)中提取数据并将数据流式传输到响应.我打算使用普通的NSURLConnection
,并且在NSURLConnection
回调的didReceiveData
中,我想将此数据传递给GCDWebServerResponse
.
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
.
我很难弄清楚如何才能保持打开请求的连接,以便可以使用NSURLConnection
发起另一个请求,并开始将数据提供给响应.
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.
有什么办法可以做到吗?我需要创建一个新的GCDWebServerStreamedResponse
子类吗?
Is there any way I can do that? Do I need to create a new subclass of GCDWebServerStreamedResponse
?
谢谢.
推荐答案
您无需将GCDWebServerStreamedResponse
子类化,只需实例化它并使用GCDWebServerAsyncStreamBlock
回调即可.
You don't need to subclass GCDWebServerStreamedResponse
but simply instantiate it and use the GCDWebServerAsyncStreamBlock
callback.
在回调中,创建您的NSURLConnection
并使其异步运行.然后,只要有新数据可用(例如,来自-didReceiveData:
的数据),就使用GCDWebServerBodyReaderCompletionBlock
将其传递通过,而当没有更多数据可用时,传递一个空的NSData.
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.
有关类似内容,请参见 GCDWebServer自述文件中的高级异步版本".概念.
See the "Advanced asynchronous version" in the GCDWebServer README for a similar concept.
这篇关于带GCDWebServer的iOS重新路由请求(不重定向)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!