有了websocket还用不用http?使用websocket还是http?
有一篇很全面的文章, 其中讲到了websocket和http/2的对比(http/1 不支持‘服务器端事件SSE’) : https://blog.sessionstack.com/how-javascript-works-deep-dive-into-websockets-and-http-2-with-sse-how-to-pick-the-right-path-584e6b8e3bf7
文中比较websocket和http的部分的大意:
websocket请求头很小, 数据帧(frame)控制也很简单, 所以低延时(low latency)
由于websocket是先由http连接后, 发送upgrade请求后“升级”上去的一个新连接, 所以在安全性, 负载均衡等方面, 不如http/https
优点
WebSockets will certainly survive the domination of HTTP/2 + SSE, mainly because it’s a technology already well adopted and, in very specific use cases, it has an advantage over HTTP/2 as it has been built for bidirectional capabilities with less overhead (e.g. headers).
Say you want to build a Massive Multiplayer Online Game that needs a huge amount of messages from both ends of the connection. In such a case, WebSockets will perform much, much better.
In general, use WebSockets whenever you need a truly low-latency, near realtime connection between the client and the server. Keep in mind that this might require rethinking how you build your server-side applications, as well as shifting the focus on technologies like event queues.
缺点
WebSockets can often be a source of pain when considering compatibility with existing web infrastructure as it upgrades an HTTP connection to a completely different protocol that has nothing to do with HTTP.
Scale and security: Web components (Firewalls, Intrusion Detection, Load Balancers) are built, maintained and configured with HTTP in mind, an environment that large/critical applications will prefer in terms of resiliency, security, and scalability.