目前我在显示从我的Web服务Node.js服务器(localhost:3000)发送到Node.js服务器上运行的模拟客户端(localhost:3001)的"响应块"时出现问题.
edit* - 当前实现只使用Angular的%http作为没有web-socket的传输
逻辑如下:
1.在"城市"的客户端创建一个数组,并将它们(从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(数据),它以"块"传回数据.
旁注 - 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* } ]
目前问题不在于'chunked'响应没有到达目的地,而是我不知道一旦块进入就开始处理数据的方法.
这是一个问题,因为我试图做一个250x250的矩阵并等待完整响应重载Angular能够显示结果,因为它试图一次完成所有这些(从而炸毁页面).
这也是一个问题,因为我试图将响应保存到MongoDB,它只能处理一定大小的数据才能使MongoDB处理"太大".
我试过调查Angular的$ q和promise/defer API,但我对如何实现它有点困惑,并且没有找到一种方法来开始处理数据块.
这个关于处理块的问题似乎也没有多大帮助.
任何帮助或提示尝试显示返回AngularJS的分块数据将不胜感激.
如果回复可以是展示该技术的信息性代码片段,我会非常感激,因为看到一个例子可以帮助我学习更多的"文本"描述.
- 谢谢