彗星,responseText的和内存使用情况 [英] Comet, responseText and memory usage

查看:142
本文介绍了彗星,responseText的和内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法来清除一个XHR对象的responseText不破坏XHR对象?

Is there a way to clear out the responseText of an XHR object without destroying the XHR object?

我需要保持一个持久连接打开到Web服务器实时数据馈送到浏览器。现在的问题是,有一个比较大的量的数据通过(每秒几百KΩ不断)到来,所以内存使用是个大问题,因为这种连接必须保持开放达至少若干分钟。 responseText的受到很大的速度非常快,即使我发回的JSON已经嘎吱嘎吱小,因为它可以得到的。

I need to keep a persistent connection open to a web server to feed live data to a browser. The problem is, there is a relatively large amount of data coming through (several hundred K per second constantly), so memory usage is a big problem, because this connection must remain open for at least several minutes. responseText gets very big very quickly, even though the JSON I send back has been crunched as small as it can get.

由于服务器端应用程序的工作方式,如果我使用AJAX风格的短轮询,只是销毁XHR对象时,我与它正在做的,我甚至在几毫秒错过显著大量的重要数据需要解析响应,创建一个新的XHR并将其发送出去。我没有使用重叠请求选项,作为Web服务器只接受一次一个连接。 (不要问。)所以彗星正是我所需要的模式。

Due to the way the server-side app works, if I use AJAX-style short polling and just destroy the XHR object when I'm done with it, I miss significant amounts of important data even in the few milliseconds it takes to parse the response, create a new XHR and send it out. I do not have the option to use overlapping requests, as the web server only accepts one connection at a time. (Don't ask.) So Comet is exactly the model I need.

我想这样做的是分析每个JSON块,因为它来自于服务器返回,然后清除responseText的,这样我可以继续使用相同的连接。然而,responseText的是只读。它不能直接排空由我已发现的任何方法

What I would like to do is parse each JSON chunk as it comes back from the server, and then clear out responseText so that I can keep using the same connection. However, responseText is read-only. It cannot be directly emptied by any method I have found.

有没有图片,我在这里缺少的一部分吗?没有人知道任何技巧,我可以用它来释放responseText的,当我读完了吗?或者是有其他地方的服务器响应可以去吗?

Is there a part of the picture I am missing here? Does anyone know any tricks I can use to free up responseText when I'm done reading it? Or is there another place the server responses can go?

我不包括code,因为这的确是几乎code无关的问题。 JavaScript的例程产卵XHRs和处理返回的数据是非常,非常简单。

I am not including code because this is really almost a code-agnostic question. The Javascript routines that spawn the XHRs and handle the returned data are very, very simple.

推荐答案

这就是长轮询是如何工作。你让一个索引读取的最后一个行号,并与您的间隔的每个刻度从该点开始读。这是一个长的连接,从而一个长的响应。

That's just how long-polling works. You keep an index into the last line number read and with each tick of your interval read from that point onward. It's one long connection, thus one long response.

一个新的的responseText 将意味着一个新的连接。但它不会是彗星了;)

A fresh responseText would mean a fresh connection. But then it wouldn't be comet anymore ;)

这篇关于彗星,responseText的和内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆