问题描述
我正在使用来自 AlamoFire documentation 的流式请求:
AF.streamRequest("http://myserver/stream_data"){ urlRequest in
urlRequest .timeoutInterval = 50
}.responseStreamString{ stream in
print(stream.value)
switch stream.event {
case let .stream(result):
switch result {
case let .success(data):
print(data)
}
case let .complete(completion):
print(completion.metrics)
print("done getting streaming data")
}
}
在服务器端,我使用 Flask,只是为了测试一下:
@app.route('/stream_data')
def stream_data():
app.logger.info('starting data streaming...')
def generate():
# create and return your data in small parts here
for i in range(10):
yield f"{i}\n"
time.sleep(1)
app.logger.info('done streaming data.')
return Response(stream_with_context(generate()))
在使用 curl 或浏览器(响应为 200)时,我可以看到逐渐出现的数据,但在从 xcode(真实设备)运行的 iphone 应用程序上,我出现了不稳定的行为。
有时我会看到数据流成块(不流畅),有时直到流结束我才看到任何东西,有时请求似乎挂起并最终超时。
欢迎提出建议。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)