iframe 中的 comet 服务器数据只是累积吗?
我使用分块数据将 [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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
擦除脚本标签
PS:当你想擦除脚本标签时,可能会遵循iframe 中的 comet 服务器数据只是累积吗?
我相信你应该 一段时间后关闭连接(不好,请参阅iframe 中的 comet 服务器数据是否会累积? 相反),它会自动释放与该请求关联的内存。然后您当然需要重新连接。 此页面甚至还说了一些其他内容:
这建议重新加载页面,这也是一个选项。但我认为关闭该连接(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:
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.
无需重新连接即可擦除脚本标签
您可以通过在服务器打印块时添加一些代码,在每次执行脚本标签时删除脚本标签。
压缩版本
但是,正如 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.
Compressed version
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).