为什么要使用 websocket,使用它有什么好处? [英] Why to use websocket and what is the advantage of using it?

查看:62
本文介绍了为什么要使用 websocket,使用它有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试阅读一些文章,但对这个主题不是很清楚.

有人愿意向我解释以下几点:

  • 为什么在 http 上使用 websocket
  • 什么是全双工通信
  • 低延迟交互是什么意思

解决方案

为什么要通过 http 使用 websocket?

webSocket 是客户端和服务器之间的持续连接.该连续连接允许以下内容:

  1. 数据可以随时从服务器发送到客户端,客户端甚至不需要它.这通常称为服务器推送,对于客户端需要快速知道服务器上发生的事情(例如收到新的聊天消息或更新的新价格)的应用程序非常有价值.客户端无法通过 http 推送数据.客户端必须通过每隔几秒发出一次 http 请求来定期轮询,以便及时获取新数据.客户端轮询效率不高.

  2. 数据可以非常高效地发送.因为已经建立了连接并且 webSocket 数据帧的组织非常有效(主要是 6 个额外字节,2 个字节用于标头,4 个字节用于掩码),与通过必然包含标头的 HTTP 请求相比,发送数据的效率要高得多,饼干等...

<块引用>

什么是全双工通信?

全双工意味着数据可以随时通过连接以任一方式发送.

<块引用>

低延迟交互是什么意思

低延迟意味着从您请求某事的时间到您得到响应的时间之间几乎没有延迟.由于它适用于 webSockets,它只是意味着可以更快地发送数据(特别是通过慢速链接),因为连接已经建立,因此不需要额外的数据包往返来建立 TCP 连接.

要比较通过 http 请求发送一些数据与已建立的 webSocket 连接所涉及的内容,请参阅此答案中列出的步骤:用于实时数据的 websocket 与 rest API?

这些其他参考资料也可能有用:

每当函数调用:Ajax 或 WebSockets

对于推送通知,websocket 是强制性的吗?

HTML5 WebSocket:Web 可扩展性的量子飞跃

I tried reading some articles, but not so clear on this topic.

Would someone like to explain me below points:

  • Why use websocket over http
  • what is full duplex communication
  • what do you mean by lower latency interaction

解决方案

Why use websocket over http?

A webSocket is a continuous connection between client and server. That continuous connection allows the following:

  1. Data can be sent from server to client at any time, without the client even requesting it. This is often called server-push and is very valuable for applications where the client needs to know fairly quickly when something happens on the server (like a new chat messages has been received or a new price has been udpated). A client cannot be pushed data over http. The client would have to regularly poll by making an http request every few seconds in order to get timely new data. Client polling is not efficient.

  2. Data can be sent either way very efficiently. Because the connection is already established and a webSocket data frame is very efficiently organized (mostly 6 extra bytes, 2 bytes for header and 4 bytes for Mask), one can send data a lot more efficiently than via a HTTP request that necessarily contains headers, cookies etc...

what is full duplex communication?

Full duplex means that data can be sent either way on the connection at any time.

what do you mean by lower latency interaction

Low latency means that there is very little delay between the time you request something and the time you get a response. As it applies to webSockets, it just means that data can be sent quicker (particularly over slow links) because the connection has already been established so no extra packet roundtrips are required to establish the TCP connection.

For a comparison in what's involved to send some data via an http request vs. an already established webSocket connection see the steps listed in this answer: websocket vs rest API for real time data?

These other references may also be useful:

Server-push whenever a function is called: Ajax or WebSockets

For a push notification, is a websocket mandatory?

HTML5 WebSocket: A Quantum Leap in Scalability for the Web

这篇关于为什么要使用 websocket,使用它有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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