java 为什么要使用 websocket,使用它有什么好处?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44898882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why to use websocket and what is the advantage of using it?
提问by JR Sahoo.'JS'
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
- 为什么在 http 上使用 websocket
- 什么是全双工通信
- 低延迟交互是什么意思
回答by jfriend00
Why use websocket over http?
为什么在 http 上使用 websocket?
A webSocket is a continuous connection between client and server. That continuous connection allows the following:
webSocket 是客户端和服务器之间的持续连接。该连续连接允许以下内容:
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.
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...
数据可以随时从服务器发送到客户端,甚至不需要客户端请求。这通常称为服务器推送,对于客户端需要快速知道服务器上发生的事情(例如收到新的聊天消息或更新的新价格)的应用程序非常有价值。客户端无法通过 http 推送数据。客户端必须通过每隔几秒发出一次 http 请求来定期轮询,以便及时获取新数据。客户端轮询效率不高。
数据可以非常有效地以任何一种方式发送。因为已经建立了连接并且非常有效地组织了 webSocket 数据帧,所以可以通过必须包含标头、cookie 等的 HTTP 请求更有效地发送数据......
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.
低延迟意味着您请求某事的时间和您得到响应的时间之间的延迟非常小。由于它适用于 webSockets,它只是意味着可以更快地发送数据(特别是通过慢速链接),因为连接已经建立,因此不需要额外的数据包往返来建立 TCP 连接。
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?
要比较通过 http 请求发送一些数据与已建立的 webSocket 连接所涉及的内容,请参阅此答案中列出的步骤: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
Push notification | is websocket mandatory?