iframe 中的 comet 服务器数据只是累积吗?

发布于 2024-11-19 09:46:03 字数 103 浏览 2 评论 0原文

我使用分块数据将 [script]dosomething()[/script] 标签推送到我的 comet 服务器的 iframe 中,但脚本标签只是继续永远累积。如何在每个脚本标签后擦除它?

I push [script]dosomething()[/script] tags into the iframe for my comet server using chunked data, but script tags just continues to accumulate forever. How do I wipe it after every script tag?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世态炎凉 2024-11-26 09:46:03

擦除脚本标签

PS:当你想擦除脚本标签时,可能会遵循iframe 中的 comet 服务器数据只是累积吗?

我相信你应该 一段时间后关闭连接不好,请参阅iframe 中的 comet 服务器数据是否会累积? 相反),它会自动释放与该请求关联的内存。然后您当然需要重新连接。 此页面甚至还说了一些其他内容:

“页面流”是指浏览器
发现服务器变化差不多
立即地。这将打开
实时更新的可能性
浏览器,并允许
双向信息流。
然而,这与
标准 HTTP 使用,这导致
几个问题。首先,有
不幸的记忆影响,
因为Javascript保留
累积,浏览器必须
将所有内容保留在其页面模型中。
在具有大量内容的丰富应用程序中
更新,该模型将会增长
很快,在某个时刻,一个页面
需要刷新才能
避免硬盘驱动器交换,或更糟糕的情况
命运。

这建议重新加载页面,这也是一个选项。但我认为关闭该连接(iframe)并重新连接也可能有效。

Comet 有很多问题需要解决:

这就是为什么我再次推荐您使用socket.io(见下文)处理所有这些废话。

Socket.io

我建议你使用 socket.io 代替,这是非常好的产品。 所有主流浏览器都支持。正如您所看到的,它支持多种传输(XHR、Websockets 等),并选择浏览器上可用的最佳传输以获得最佳性能。

Wipe script tag

P.S: When you want to wipe script tags it is probably to follow Does comet server data in the iframe just accumulate?

I believe you should close the connection after sometime(no good, see Does comet server data in the iframe just accumulate? instead) which automatically frees up the memory associated with that request. You then off course need to reconnect. This page says something else even:

"Page Streaming" means the browser
discovers server changes almost
immediately. This opens up the
possibility of real-time updates in
the browser, and allows for
bi-directional information flow.
However, it's quite a departure from
standard HTTP usage, which leads to
several problems. First, there are
unfortunate memory implications,
because the Javascript keep
accumulating, and the browser must
retain all of that in its page model.
In a rich application with lots of
updates, that model is going to grow
quickly, and at some point a page
refresh will be necessary in order to
avoid hard drive swapping, or a worse
fate.

This advices to reload the page which is also an option. But I think closing that connection(iframe) and reconnecting might also work.

Comet has a lot of problems you need to hack around:

That's why I again recommend you to use socket.io(see below) which takes care of all this nonsense.

Socket.io

I advice you to use socket.io instead, which is very good product. It is supported by all major browsers. As you can see it support a lot of transport(XHR, Websockets, etc) and choices the best one available on your browser for the best performance.

_畞蕅 2024-11-26 09:46:03

无需重新连接即可擦除脚本标签

您可以通过在服务器打印块时添加一些代码,在每次执行脚本标签时删除脚本标签。

<script type="text/javascript">
// Calls your message handler
app.handle("Hello World");

// Removes this script element
var scripts = document.getElementsByTagName("script"),
    elem = scripts[scripts.length - 1];

elem.parentNode.removeChild(elem);
</script>

压缩版本

<script type="text/javascript">
app.handle("Hello World");
(function(){var a=document.getElementsByTagName("script"),a=a[a.length-1];a.parentNode.removeChild(a)})();
</script>

但是,正如 Alfred 提到的,Hidden Iframe 或 Forever Iframe 太烦人了,无法使用。就我个人而言,我认为这种古典的方式让彗星看起来毫无优雅和魅力。

jQuery Stream

我的推荐是使用 jQuery Stream,它提供了统一的双向基于WebSocket和HTTP协议的通信接口。它是一个轻量级的客户端 JavaScript 库,例如 jQuery。

jQuery Stream 使用的增强型 Iframe 传输在很多方面与经典传输不同,它要求仅包含消息而不是文本/html 响应的文本/纯响应,并在每次处理时清空响应。

根据一些用户的测试,Internet Explorer 8 使用增强的 Iframe 传输对于几兆字节的消息没有问题(不像 Firefox 使用 XMLHttpRequest 作为传输,这确实很困难)。

Wipe script tag without reconneting

You can remove script tag every time that it is executed by adding some code when the server prints chunk.

<script type="text/javascript">
// Calls your message handler
app.handle("Hello World");

// Removes this script element
var scripts = document.getElementsByTagName("script"),
    elem = scripts[scripts.length - 1];

elem.parentNode.removeChild(elem);
</script>

Compressed version

<script type="text/javascript">
app.handle("Hello World");
(function(){var a=document.getElementsByTagName("script"),a=a[a.length-1];a.parentNode.removeChild(a)})();
</script>

But, Hidden Iframe or Forever Iframe is too annoying to use as Alfred mentioned. Personally, I think this classical way makes Comet look graceless and charmless.

jQuery Stream

My recommendation is to use jQuery Stream, which provides the unified two-way communication interface over WebSocket and HTTP protocol. It is a light-weight client-side JavaScript Library such as jQuery.

The enhanced Iframe transport being used by jQuery Stream is different from the classical one in many ways, requries text/plain response containing only messages instead of text/html response and empties the response every time that it's handled.

According to some user's test, Internet Explorer 8 using enhanced Iframe transport has no problem with messages of several megabytes (unlike Firefox using XMLHttpRequest as transport, which is really struggling).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文