Forever-frame和服务器发送的事件之间有什么区别? [英] What is the difference between Forever-frame and server sent events?

查看:109
本文介绍了Forever-frame和服务器发送的事件之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与这个问题确实很相似:

This question is really similar with this question:What is the difference between web sockets, long polling, server-sent events and forever frame?

但是,这个问题的答案没有提到SSE和Forever-frame之间的区别.
让我给您简要解释一下.

However, this question's answer doesn't mention the difference between SSE and Forever-frame.
Let me give you brief explanation about them.

关于SSE,该系统确实类似于Comet,但与Comet不同的一点是,在发送数据后不会断开连接. 因此,从服务器到客户端的连接是长期有效的,并且客户端会接收到整个数据的一系列片段.

Regarding to SSE, the system really resembles Comet, but the point which is different with Comet is not breaking the connection after data being sent. So, connection from a server to a client is long-alive and the client receives series of fragments of a whole data.

另一方面,永远的框架似乎与我非常相似.在Forever框架中,客户端首先收到包含iframe标签的页面,从而在隐藏的iframe中建立了长期连接.然后,客户端从服务器接收分块的数据,并使用客户端已拥有的第一个文档上的某些功能来操作DOM.

On the other hand, forever frame seems to be very similar to me. In Forever frame, first the client receives the page including an iframe tag, establishing a long-lived connection inside the hidden iframe. And then the client receives chunked datas from the server and manipulates the DOM with some functions on the first document the client already has.

我认为区别在于Forever-frame在机制中使用了iframe标签,但SSE没有,SSE可以通过更多方式实现. 我说的对吗?

I assume the difference is Forever-frame uses iframe tag in the mechanism, but SSE doesn't and SSE can be realized by more ways. Am I right?

推荐答案

我以前从未听说过这个名称的Forever-frame! (在"iframe"部分的我的书带有HTML5 SSE的数据推送应用程序"的第7章中对此进行了介绍).

I'd not heard of Forever-frame by that name before! (It is covered in ch.7 of my book, Data Push Apps with HTML5 SSE, in the "iframe" section).

长期轮询:发出请求(使用XMLHttpRequest,即ajax),使其保持打开状态,直到服务器上的数据准备就绪为止.然后插座关闭.重新连接以获取下一点数据.

Long-polling: make a request (using XMLHttpRequest, i.e. ajax), keep it open until the data is ready on the server. Then the socket closes. Re-connect to get the next bit of data.

XHR轮询:发出请求(使用XMLHttpRequest2,即ajax),但侦听readyState == 3信号.后端服务器必须知道保持连接打开,而客户端必须知道侦听readyState == 3. (在IE9和更早版本中不起作用,因为该浏览器从不发送readyState == 3消息,而是直接进入readyState == 4)

XHR polling: make a request (using XMLHttpRequest2, i.e. ajax), but listen in to the readyState==3 signals. The back-end server has to know to keep the connection open, and the client has to know to listen to readyState==3. (Does not work in IE9 and earlier, because that browser never delivers the readyState==3 messages, but goes straight to readyState==4)

iframe::打开一个隐藏的iframe进入后端进程.定期查看iframe的源代码,看看是否有新内容. (从技术上讲,它可以在所有浏览器上使用,但是IE8和IE9是我(2013年)测试中仅有的,具有足够低延迟以使更新有用的测试.)

iframe: open a hidden iframe to the back-end process. Regularly go look at the source code of the iframe, and see if anything new is there. (Technically it works on all browsers, but IE8 and IE9 were the only ones in my (2013) tests with low enough latency for the updates to be useful.)

SSE :一种HTML5标准,其基本原理类似于XHR轮询(至少在Firefox和Chrome中,直接在XMLHttpRequest2之上实现),但是隐藏了复杂的细节.如果套接字断开,它还会添加自动重新连接功能,以及诸如此类的其他一些高级功能.

SSE: An HTML5 standard that basically works like XHR polling (Firefox and Chrome, at least, implement it directly on top of XMLHttpRequest2), but with the complex details hidden from you. It also adds auto-reconnect if the socket goes down, and a few other high-level features like that.

在上述书籍的第7章结尾处,我展示了使所有技术都可以在几乎任何浏览器中工作的代码.优先顺序为:

At the end of chapter 7 of the afore-mentioned book, I show code to get all the techniques to work in just about any browser. The order of preference is:

  • 上交所(如果有)
  • 其他XHR(如果可用)
  • 如果是IE8或IE9,则为其他iframe
  • 其他longpoll

还有另一个区别:XHR和iframe技术将整个消息历史记录存储在内存中.因此,如果您打算长时间保持套接字打开状态和/或发送大量大消息,则可能会导致内存问题,而SSE不会发生这种情况.

There is one other difference: the XHR and iframe techniques are storing the entire message history in memory. So, if you intend to keep the socket open for a long time and/or send a lot of large messages, this may cause a memory issue that wouldn't happen with SSE.

执行摘要:除非您有足够多的客户仍在使用IE8/IE9,而longpoll方法会给您的基础架构带来明显的额外负载,否则请不要担心永久帧".

Executive Summary: Don't worry about "forever-frame" unless you have enough customers still using IE8/IE9 that the longpoll approach would create noticeable additional load on your infrastructure.

这篇关于Forever-frame和服务器发送的事件之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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