简体   繁体   English

服务器发送事件vs Web套接字?

[英]Server sent event vs web sockets?

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. 我正在开发一个网络应用程序,用户可以通过从智能手机到桌面的多个平台访问这些应用程序,有时需要在两个客户端之间进行通信,例如,如果我希望我的朋友加入我的网络,我会向他发送朋友请求但是我希望我的朋友能够看到该请求而无需刷新页面。

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? 有更好的选择吗?

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). 尝试通过WebSocket连接获取内容是一个糟糕的设计决策,因为WebSockets是嵌套在HTTP连接中的不同协议,它不能利用缓存(浏览器和CDN)。
  • 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. 某些较旧的代理不会传递Websocket连接,除非它隐藏在安全连接中,而Server Sent Events仍然是HTTP连接,并且不会受此影响。
  • 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. 在4.4之前(当他们切换到使用Chrome时)本机Android浏览器中既不支持WebSockets也不支持SSE - 因此,如果您正在考虑使用混合移动应用程序,则需要回退,例如SocketIO,因为在撰写本文时,4.4是只有20%的市场和混合应用程序使用原生Android浏览器。
  • 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. 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/

  • 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. 服务器发送事件:仅用于发送文本消息的持久连接服务器2-客户端,并且在所有主要浏览器中实现,但是Internet Explorer。 It can reconnect itself if connectivity is lost. 如果连接丢失,它可以重新连接。 http://caniuse.com/eventsource http://caniuse.com/eventsource

  • WebSokets: A full duplex persistent connection capable of transmitting UTF8 text and binary data. WebSokets:能够传输UTF8文本和二进制数据的全双工持久连接。 http://caniuse.com/websockets http://caniuse.com/websockets

WebSocket is better, and the future. WebSocket更好,未来。

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. 据我所知,SSE更简单,更容易实现,而WebSockets提供双向数据传输,但是您需要了解自己的协议/ API以利用它们。 Honestly I've never really bothered with SSEs, Socket.IO does all I've needed as far as real time web app communication fairly easily and is built to be cross-browser. 老实说,我从来没有真正打过SSE, Socket.IO确实需要我所需要的所有实时网络应用程序通信相当容易,并构建为跨浏览器。

If you just want him to be able to see a notification, then SSEs should be fine. 如果您只是希望他能够看到通知,那么SSE应该没问题。 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. 如果您希望他能够从同一页面回复您的朋友请求,那么让服务器向您发送他已被接受的通知,您可能希望使用WebSockets实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM