WebSockets vs. Server-Sent events/EventSource
WebSocket和服务器发送的事件都能够将数据推送到浏览器。在我看来,它们似乎是相互竞争的技术。他们之间有什么区别?你什么时候会选择一个而不是另一个?
websockets ESS(服务器和事件都是学院派)capable日期推到browsers competing不管一个人多,他们不是技术。P></
websockets connections can both to the send和receive日期日期从浏览器浏览器。好的,我可以使用Application of an example is a websockets聊天应用。P></
ESS connections can only to the浏览器推送数据。网上证券quotes时间12或饲料,或twitters examples of an application that are good我从ESS的效益。P></
因为一切在实践that can be done with be done with websockets ESS can also is websockets,让很多的关注和更多的爱和更多的支持,比多browsers websockets ESS。P></
不管一个人多,它可以overkill for some of the types and Application,后端是实施"订单easier to such as ESS与协议。P></
furthermore ESS can be that do not into polyfilled老年browsers支持它只是natively使用JavaScript。implementations some of polyfills can be found on the ESS modernizr GitHub页面。P></
陷阱:P></
- ESS的限制suffers from a to the maximum number of open connections,which can be when opening specially痛苦的各种tabs as the limit是浏览器和非常低的集数(6)。has been marked as the问题"不固定"在Chrome和Firefox
- 我只transmit WS和二进制数据都是UTF-8,ESS公司的UTF - 8。(thanks to茶道nihi)。
html5rocks has some good在线信息系统。主页:from thatP></
Server-Sent Events vs. WebSockets
Why would you choose Server-Sent Events over WebSockets? Good question.
One reason SSEs have been kept in the shadow is because later APIs like WebSockets provide a richer protocol to perform bi-directional, full-duplex communication. Having a two-way channel is more attractive for things like games, messaging apps, and for cases where you need near real-time updates in both directions. However, in some scenarios data doesn't need to be sent from the client. You simply need updates from some server action. A few examples would be friends' status updates, stock tickers, news feeds, or other automated data push mechanisms (e.g. updating a client-side Web SQL Database or IndexedDB object store). If you'll need to send data to a server, XMLHttpRequest is always a friend.
SSEs are sent over traditional HTTP. That means they do not require a special protocol or server implementation to get working. WebSockets on the other hand, require full-duplex connections and new Web Socket servers to handle the protocol. In addition, Server-Sent Events have a variety of features that WebSockets lack by design such as automatic reconnection, event IDs, and the ability to send arbitrary events.
摘要:TLDR
优点:websockets过大学ESSP></
- transported instead of a custom过简单的http协议
- JavaScript可以充满了"聚在后面门廊的ESS browsers that do not to"还要支持它。
- 在重新连接支持for新建和事件ID
- 简单的内部协议
优点:websockets过大学ESSP></
- 实时通信,两个方位。
- 在browsers原生支持黑莓
使用案例:理想大学ESSP></
- 流股的股票
- Twitter的饲料12
- notifications to浏览器
ESS的陷阱:P></
- 不支持二进制
- 最大极限open connections
根据caniuse.com:P></
- 90 %的全球用户natively支持websockets
- 84 %的全球用户支持服务器natively"事件
你可以使用在客户端只支持polyfill to extend to many other of browsers ESS。likely with this is less websockets。polyfills:eventsource someP></
- eventsource by with no other雷米夏普(IE7 + dependencies)图书馆
- jquery.eventsource by瑞克?
- eventsource模式yaffle(replaces normalising行为在本地执行,browsers)
如果你在browsers the need to Support Library,考虑使用网页JS类插座,支持多条signalr or which socket.io such as websockets,ESS和Ajax,永远长帧配置。这些modifications often require to the服务器端为好。P></
ESS:learn about more fromP></
- HTML5的岩石条
- W3C规格(the draft version发表,editor' S)
从websockets learn about:黑莓P></
- HTML5的岩石条
- W3C规格(the draft version发表,editor' S)
其他的不同:P></
- websockets supports任意二进制数据,只uses ESS utf - 8
歌剧,Safari浏览器,supports ESS,Chrome,Safari supports ESS sharedworker inside of火狐浏览器的XMLHttpRequest supports readystate互动,可以让我们知道eventsource polyfil for firefoxP></
WebSocket与SSE
Web套接字-它是一种协议,通过单个TCP连接提供全双工通信通道。例如,服务器和浏览器之间的双向通信由于协议比较复杂,服务器和浏览器必须依赖WebSocket库。是
1 | Example - Online chat application. |
SSE(服务器发送事件)-如果发生服务器发送事件,则仅在服务器与浏览器之间进行通信,并且浏览器无法向服务器发送任何数据。这类通信主要用于当只需要显示更新的数据时,服务器就会在数据更新时发送消息。例如,服务器与浏览器之间的单向通信。该协议不那么复杂,因此无需依赖外部库javascript本身提供
1 | Example - Online stock quotes or cricket score website. |
注释:一个thing to我公司是一websockets和有问题的。(helps but not总是使用HTTPS)。P></
HTTPS协议:github.com learnboost / / / / / socket.io-and-firewall-software socket.io维基HTTPS:/ / / / github.com sockjs sockjs -客户/问题/ 94P></
这些假设是有问题的服务器与T as many"事件。但我不知道。P></
我是说,websockets运行吨。我的小游戏网站,uses websockets(socket.io)(http://minibman.com)P></
下面是关于Web套接字和服务器发送事件之间的区别的讨论。自从JavaEE 7以来,WebSoStAPI已经成为规范的一部分,并且似乎服务器发送的事件将在企业版的下一版本中发布。
最大连接限制不是http2+sse的问题。
这是HTTP 1上的一个问题