目前,我在显示从 Web 服务 Node.js 服务器 (localhost:3000) 发送到 Node.js 服务器 (localhost:3001) 上运行的模拟客户端的响应“块”时遇到问题。
- 编辑 * - 当前实现仅使用 Angular 的 %http 作为传输,没有网络套接字
逻辑如下:
1.在“Cities”的客户端创建一个数组并将它们(从 AngularJS 控制器)发布到位于以下位置的 Web 服务: localhost:3000/getMatrix
$http({
method: 'POST',
url: 'http://localhost:3000/getMatrix',
data: cityArray
}).
success(function (data,status,headers,config){
// binding of $scope variables
// calling a local MongoDB to store the each data item received
for(var key in data){
$http.post('/saveRoutes', data[key])
.success(function (data, status){
// Data stored
})
.error(function (data, status){
// error prints in console
});
}
}).
error(function (data,status,headers,config){
alert("Something went wrong!!");
});
2.然后,Web 服务运行其流程来创建“城市”矩阵(例如,如果传递了 5 个城市,它将返回 5by5 [25 项] 的 JSON 矩阵)。但问题是,由于 Node 的 > response.write(data )
旁注 - Node.js 自动在标头中设置 'Transfer-Encoding':'chunked'
* Other code before (routing/variable creation/etc.) *
res.set({
'Content-Type':'application/json; charset=utf-8',
});
res.write("[\n");
* Other code to process loops and pass arguments *
// query.findOne to MongoDB and if there are no errors
res.write(JSON.stringify(docs)+",\n");
* insert more code to run loops to write more chunks *
// at the end of all loops
res.end("]");
// Final JSON looks like such
[
{ *data* : *data* },
{ *data* : *data* },
......
{ *data* : *data* }
]
目前的问题是not“分块”响应没有到达目的地,但我不知道如何在块进入后立即开始处理数据。
这是一个问题,因为我试图做一个 250x250 的矩阵,等待完整的响应会导致 Angular 显示结果的能力超载,因为它试图一次完成所有操作(从而炸毁页面)。
这也是一个问题,因为我试图保存对 MongoDB 的响应,并且它只能处理一定大小的数据,直到数据“太大”而无法让 MongoDB 处理。
我尝试过研究 Angular 的$q和承诺/延迟 API,但我对如何实现它有点困惑,并且还没有找到一种方法来开始处理传入的数据块。
这个问题关于处理块的问题似乎也没有多大帮助。
任何有关尝试在返回 AngularJS 时显示分块数据的帮助或提示将不胜感激。
如果响应可以是演示该技术的信息丰富的代码片段,我将非常感激,因为看到示例可以帮助我了解比“文本”描述更多的内容。
- 谢谢