使用HTTP/2时,我们是否应该在Websocket上选择SSE + REST? [英] Should we prefer SSE + REST over websocket when using HTTP/2?

查看:221
本文介绍了使用HTTP/2时,我们是否应该在Websocket上选择SSE + REST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用websocket时,我们需要专用的连接来进行双向通信.如果我们使用http/2,则服务器将维护第二个连接.

When using websocket, we need a dedicated connection for bidirectionnel communication. If we use http/2 we have a second connection maintained by the server.

在那种情况下,使用websocket似乎会带来不必要的开销,因为使用SSE和常规的http请求,我们可以在单个HTTP/2连接上进行双向通信.

In that case, using websocket seems to introduce an unecessary overhead because with SSE and regular http request we can have the advantage of bidirectionnal communication over a single HTTP/2 connection.

您怎么看?

推荐答案

在一个多路复用的HTTP/2 TCP连接中使用2个流(一个流用于服务器到客户端的通信-

Using 2 streams in one multiplexed HTTP/2 TCP connection (one stream for server-to-client communication - Server Sent Events (SSE), and one stream for client-to-server communication and normal HTTP communication) versus using 2 TCP connections (one for normal HTTP communication and one for WebSocket) is not easy to compare.

里程可能会因应用程序而异.

Probably the mileage will vary depending on applications.

开销?好吧,当然,连接的数量增加了一倍. 但是,WebSocket可以压缩消息,而SSE无法.

Overhead ? Well, certainly the number of connections doubles up. However, WebSocket can compress messages, while SSE cannot.

灵活性?如果连接是分开的,则它们可以使用不同的加密. HTTP/2通常需要非常强的加密,这可能会限制性能. 另一方面,WebSocket不需要TLS.

Flexibility ? If the connections are separated, they can use different encryptions. HTTP/2 typically requires very strong encryption, which may limit performance. On the other hand, WebSocket does not require TLS.

明文WebSocket是否可以在移动网络中工作?根据我的经验,这取决于.防病毒软件,应用程序防火墙,移动运营商可能会限制WebSocket流量,或使其可靠性下降,具体取决于您所运营的国家/地区.

Does clear-text WebSocket work in mobile networks ? In the experience I have, it depends. Antiviruses, application firewalls, mobile operators may limit WebSocket traffic, or make it less reliable, depending on the country you operate.

API可用性? WebSocket是更广泛的部署和公认的标准;例如,在Java中,有一个正式的API(javax.websocket),另一个正在发布(java.net.websocket).

API availability ? WebSocket is a wider deployed and recognized standard; for example in Java there is an official API (javax.websocket) and another is coming up (java.net.websocket).

我认为SSE在双向Web通信方面是技术上较差的解决方案,并且作为一种技术,它并不十分流行(与WebSocket相比,没有标准的API,没有书籍等). 即使它从HTML5中删除,我也不会感到惊讶,尽管它是 EventSourceServlet.java"rel =" noreferrer>在Jetty中实现.

I think SSE is a technically inferior solution for bidirectional web communication and as a technology it did not become very popular (no standard APIs, no books, etc - in comparison with WebSocket). I would not be surprised if it gets dropped from HTML5, and I would not miss it, despite being one of the first to implement it in Jetty.

根据您感兴趣的内容,您必须进行基准测试或针对特定案例评估技术.

Depending on what you are interested in, you have to do your benchmarks or evaluate the technology for your particular case.

这篇关于使用HTTP/2时,我们是否应该在Websocket上选择SSE + REST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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