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

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

问题描述

我试过阅读一些文章,但在这个主题上并不那么明确。

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

有人想在下面解释我的要点:

Would someone like to explain me below points:


  • 为什么要使用websocket over http

  • 什么是全双工通信

  • 你是什么意思低延迟交互

感谢任何形式的帮助。

推荐答案


为什么使用websocket over http?

Why use websocket over http?

webSocket是一个连续的连接客户端和服务器之间。这种连续连接允许以下内容:

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


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

  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.

可以非常有效地以任一方式发送数据。因为连接已经建立并且webSocket数据帧的组织非常有效,所以可以通过必须包含标题,cookie等的HTTP请求更有效地发送数据......

Data can be sent either way very efficiently. Because the connection is already established and a webSocket data frame is very efficiently organized, one can send data a lot more efficiently that via an 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

低延迟意味着您在请求内容和获得响应之间的延迟非常小。因为它适用于webSockets,它只是意味着可以更快地发送数据(特别是通过慢速链接),因为连接已经建立,因此不需要额外的数据包往返来建立TCP连接。

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.

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

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:

每当调用函数时服务器推送:Ajax或WebSockets

推送通知|是websocket强制性的吗?

HTML5 WebSocket :网络可扩展性的量子跃进

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

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