有什么需要,尽快为他们进入AngularJS显示“分块”响应的方式?有什么、方式、AngularJS

2023-09-13 02:56:15 作者:零落的回憶ヽ

目前我有显示那我从我的Web服务的Node.js服务器(本地主机:3000)发送响应块的问题与一的Node.js服务器上运行的模拟客户端。(本地主机:3001)

Currently I have a problem displaying 'chunks' of responses that I am sending from my Web Service Node.js server (localhost:3000) to a simulated client running on a Node.js server (localhost:3001).

编辑* - 当前实现只使用角的%HTTP传输无网络插座

逻辑去如下:搜索结果1。创建的城市的客户端的数组,并张贴他们(从AngularJS控制器)位于到Web Service:本地主机:3000 / getMatrix

The logic goes as follows: 1 . Create an array on the client side of 'Cities' and POST them (from the AngularJS controller) to the Web Service located at: 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矩阵)。但美中不足的是,它传递回'块'感谢节点的>的Response.Write数据(数据的)

2 . The Web Service then runs through its process to make a matrix of 'Cities' (eg. If it was passed 5 cities, it would return a JSON matrix of 5by5 [25 items]). But the catch is that it passes back the data in 'chunks' thanks to Node's > response.write( data )

附注 - Node.js的自动设置传送编码:分块在标题

Side note - Node.js automatically sets 'Transfer-Encoding':'chunked' in the header

* 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* }
]

结果

目前的问题是,不可以的分块响应没有到达目的地,但我不知道的方式,以尽快块进来开始处理数据。

Currently the problem is not that the 'chunked' response is not reaching its destination, but that I do not know of a way to start processing the data as soon as the chunks come in.

这是一个问题,因为我试图做的250x250的矩阵,等待充分反应重载角的,因为它试图做这一切在一次(从而炸毁页)来显示结果的能力。

This is a problem since I am trying to do a matrix of 250x250 and waiting for the full response overloads Angular's ability to display the results as it tries to do it all at once (thus blowing up the page).

这是也是自我想保存到MongoDB的响应问题,它只能处理数据具有一定规模之前,它是'太大'MongoDB的处理。

This is also a problem since I am trying to save the response to MongoDB and it can only handle a certain size of data before it is 'too large' for MongoDB to process.

我试图寻找到角的的 $ Q 和诺/推迟API ,但我对如何实现它并没有找到一种方法,开始处理数据块,因为他们进来有点糊涂了。

I have tried looking into Angular's $q and the promise/defer API, but I am a bit confused on how to implement it and have not found a way to start processing data chunks as they come in.

This在SO问题 关于处理块似乎没有多大帮助无论是。

This question on SO about dealing with chunks did not seem to help much either.

任何帮助或提示的努力,因为它回来AngularJS会大大AP preciated显示分块数据。

Any help or tips on trying to display chunked data as it comes back to AngularJS would be greatly appreciated.

如果该反应可能是情报code片段展示的技术,我将不胜AP preciate它,因为看到一个例子可以帮助我学习,而不是一个文本描述了。

If the responses could be informative code snippets demonstrating the technique, I would greatly appreciate it since seeing an example helps me learn more than a 'text' description.

- 谢谢

推荐答案

没有例子,因为我不知道您使用的运输code而言什么/如果你有一个可用的WebSocket的:

No example because I am not sure what you are using in terms of transport code/if you have a websocket available:

$ HTTP不支持做任何的回调,直到成功code。在请求结束传递回过 - 它侦听 .onreadystatechange 200 般的价值。

$http does not support doing any of the callbacks until a success code is passed back through at the end of the request - it listens for the .onreadystatechange with a 200 -like value.

如果你想要做一个流这样的你要么需要使用 $ HTTP ,并在传输层,使得多个包装它 $ HTTP 调用全部结束并返回一个成功的标题。

If you're wanting to do a stream like this you either need to use $http and wrap it in a transport layer that makes multiple $http calls that all end and return a success header.

您也可以使用WebSockets和,而不是调用 $ HTTP ,发出在插座的事件。

You could also use websockets, and instead of calling $http, emit an event in the socket.

然后,以获得块背客户机,具有服务器发出的每个块作为在后端的新事件,并有前端侦听事件和为每一个做加工

Then, to get the chunks back the the client, have the server emit each chunk as a new event on the backend, and have the front-end listen for that event and do the processing for each one.