一旦进入AngularJS,有什么方法可以显示"分块"响应?

The*_*ast 9 javascript chunked-encoding node.js angularjs q

目前我在显示从我的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!!");
});
Run Code Online (Sandbox Code Playgroud)


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* }
]
Run Code Online (Sandbox Code Playgroud)


目前问题在于'chunked'响应没有到达目的地,而是我不知道一旦块进入就开始处理数据的方法.

这是一个问题,因为我试图做一个250x250的矩阵并等待完整响应重载Angular能够显示结果,因为它试图一次完成所有这些(从而炸毁页面).

这也是一个问题,因为我试图将响应保存到MongoDB,它只能处理一定大小的数据才能使MongoDB处理"太大".

我试过调查Angular的$ qpromise/defer API,但我对如何实现它有点困惑,并且没有找到一种方法来开始处理数据块.

这个关于处理块的问题似乎也没有多大帮助.

任何帮助或提示尝试显示返回AngularJS的分块数据将不胜感激.

如果回复可以是展示该技术的信息性代码片段,我会非常感激,因为看到一个例子可以帮助我学习更多的"文本"描述.

- 谢谢

And*_*ton 3

没有示例,因为我不确定您在传输代码方面使用什么/如果您有可用的 websocket:

$http 不支持执行任何回调,直到请求结束时传回成功代码 - 它侦听带有.onreadystatechange类似200值的 。

如果您想要执行这样的流,您需要使用$http并将其包装在传输层中,该传输层进行多个$http调用,所有调用都结束并返回成功标头。

您还可以使用 websockets,而不是调用$http,而是在套接字中发出一个事件。

然后,为了将块返回给客户端,让服务器在后端将每个块作为一个新事件发出,并让前端侦听该事件并对每个块进行处理。