javascript 服务器发送事件与网络套接字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23914374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Server sent event vs web sockets?
提问by user3053234
I'm working on a web app that is accessible to users via multiple platforms from smartphones to desktops which needs to sometimes make a communication between two clients for example if I want my friend to join my network I'd send him a friend request but I want that request to be seen by my friend without him having to refresh the page.
我正在开发一个 Web 应用程序,用户可以通过从智能手机到台式机的多个平台访问该应用程序,有时需要在两个客户端之间进行通信,例如,如果我希望我的朋友加入我的网络,我会向他发送好友请求,但是我希望我的朋友无需刷新页面就能看到该请求。
In this scenario which would be a better choice? And also since I want this to work on as many platforms and browsers as possible which has more browser support? Is there a better option?
在这种情况下哪个是更好的选择?而且因为我希望它可以在尽可能多的平台和浏览器上运行,哪些浏览器支持更多?有更好的选择吗?
回答by JaysonRaymond
Some things to keep in mind when making this choice.
做出这个选择时要记住一些事情。
- Attempting to fetch content over a WebSocket connection is a poor design decision because WebSockets is a different protocol nested inside an HTTP connection and it can't leverage caching (neither the browsers nor CDNs).
- Some older proxies won't pass on a Websocket connection unless its hidden within a secure connection while Server Sent Events remains an HTTP connection and won't suffer from this.
- Neither WebSockets nor SSE are supported in the native Android browser until 4.4 (when they switched to using Chrome) - thus if you're considering a hybrid mobile app, you will need a fallback such as SocketIO since, as of this writing, 4.4 is only 20% of the market and hybrid apps use the native Android browser.
- WebSockets is the most battery efficient protocol for mobile devices, since all other options require many HTTP connections and it is the repeated negotiating of the headers that will burden the cpu and drain the battery.
- 尝试通过 WebSocket 连接获取内容是一个糟糕的设计决策,因为 WebSockets 是嵌套在 HTTP 连接中的不同协议,并且它不能利用缓存(浏览器和 CDN)。
- 一些较旧的代理不会传递 Websocket 连接,除非它隐藏在安全连接中,而服务器发送的事件仍然是 HTTP 连接并且不会受到此影响。
- 原生 Android 浏览器在 4.4 之前都不支持 WebSockets 和 SSE(当它们切换到使用 Chrome 时) - 因此,如果您正在考虑使用混合移动应用程序,您将需要一个后备,例如 SocketIO,因为在撰写本文时,4.4 是只有 20% 的市场和混合应用程序使用原生 Android 浏览器。
- WebSockets 是移动设备电池效率最高的协议,因为所有其他选项都需要许多 HTTP 连接,并且重复协商标头会给 CPU 带来负担并耗尽电池电量。
Another option may be notifications. All mobile devices now support notifications that can be targeted to an App and a number of browsers have as well. In all cases a connection already exists from the client to the messaging center (Apple, Google, Microsoft, etc) and all notifications are sent over this channel.
另一种选择可能是通知。所有移动设备现在都支持可以针对应用程序的通知,许多浏览器也支持。在所有情况下,都已经存在从客户端到消息中心(Apple、Google、Microsoft 等)的连接,并且所有通知都通过此通道发送。
Here's a good overview of WebSockets vs. SSE: http://www.html5rocks.com/en/tutorials/eventsource/basics/
这是 WebSockets 与 SSE 的一个很好的概述:http: //www.html5rocks.com/en/tutorials/eventsource/basics/
回答by vtortola
Server Sent Events: A persistent connection server-2-client only, for sending text messages and that is implemented in all major browsers, but Internet Explorer. It can reconnect itself if connectivity is lost. http://caniuse.com/eventsource
WebSokets: A full duplex persistent connection capable of transmitting UTF8 text and binary data. http://caniuse.com/websockets
服务器发送事件:仅用于发送文本消息的持久连接 server-2-client,在所有主要浏览器中实现,但 Internet Explorer。如果连接丢失,它可以重新连接自己。http://caniuse.com/eventsource
WebSokets:一种能够传输 UTF8 文本和二进制数据的全双工持久连接。http://caniuse.com/websockets
WebSocket is better, and the future.
WebSocket 更好,未来可期。
回答by rw-nandemo
From what I understand, SSEs are simpler and easier to implement, whereas WebSockets offer bi-directional data transfer but are their own protocol/API you need to understand to take advantage of. Honestly I've never really bothered with SSEs, Socket.IOdoes all I've needed as far as real time web app communication fairly easily and is built to be cross-browser.
据我所知,SSE 更简单,更容易实现,而 WebSockets 提供双向数据传输,但它们是自己的协议/API,您需要了解才能利用。老实说,我从来没有真正为 SSE 烦恼过,Socket.IO可以相当轻松地完成我需要的所有实时 Web 应用程序通信,并且构建为跨浏览器。
If you just want him to be able to see a notification, then SSEs should be fine. If you want him to be able to reply to your friend request from the same page, then have the server send you a notification that he's accepted, you'll probably want to use a WebSockets implementation.
如果您只是希望他能够看到通知,那么 SSE 应该没问题。如果您希望他能够从同一页面回复您的好友请求,然后让服务器向您发送他已被接受的通知,您可能需要使用 WebSockets 实现。